layout | title |
---|---|
page |
Questions: Type inference |
{% include JB/setup %}
Compiler reports a type mismatch between Base
and Product with Serializable with Base
.
- Where does the expected type come from?
- What is the type of
a
? Explain in terms of finding least upper bound and/or greatest lower bound calculation.
- What is the type of
b
? Explain in terms of finding least upper bound and/or greatest lower bound calculation.
-
Where is the type argument
T
forfoo
inferred: a) at assignment tores1
b) at applicationres1(2)
? -
What is the type of the expression
res1
? -
Why does the typechecker infer
Nothing
as type argument forT
? -
Why does the typechecker not infer
Any
instead ? -
foo
is a function. Yet it is partially applied and assigned to a value. What does the typechecker do for that to be possible? -
What is the type of
res2
? -
What are the constraints on the bounds of type parameter
T
while inferring type argument forbar
? -
Using your answer to the previous question explain why is the inferred type argument for
T
now different than in the case offoo
?
Compare almost identical definitions of functions foo
and bar
. Their usage (in some theoretical scenario) would be identical yet former reports a type error.
-
Is it possible to give type arguments to
foo
application, that would make the whole application correct? If yes, please state them. -
What constraint(s) will the typechecker use in order to infer type argument
U
while applyingfoo
tonew Base
? -
What constraint(s) will the typechecker use in order to infer type argument
T
while applyingfoo
tonew Base
? -
Why is the inferred type argument for
T
Nothing
? -
What constraint(s) will the typechecker use in order to infer type argument
T
while applyingbar
tonew Base
? -
What constraint(s) will the typechecker use in order to infer type constructor
U
while applyingbar
tonew Base
? -
Why is the compiler able to infer acceptable type arguments for
T
andU
but failed in the case offoo(new Base)
?
We want to define a generic partitioning function over Iterable
that takes a collection and a function which determines the split point of the collection.
- Compiler reports a type mismatch for the last tuple containing
p1
andp2
. Where is the membertakeWhile
initially defined ? Why is the type ofp1
inferred to beIterable[T]
ant notCC[T]
?
CanBuildFrom
trait defined in Scala docs allows to build collections in a generic way and it will allow us to build the desired partitioning function. CanBuildFrom[-From, -Elem, +To]
(here with type parameters and their variances) defines a way to build a collection, from a collection of type From
(e.g. Iterable[_]
) having elements of type Elem
(e.g. Float
) to a target collection To
(e.g. List[Float]
).
Notice that the only difference in signature between partition1
and partition2
is the implicit parameter and the return type.
- What is the type of
res2a
? - What are the inferred type arguments for
T
andCC
? - We do not provide the implicit argument for
cbf
directly therefore the compiler will have to infer it. Compiler will search for an implicit argument of what type? - What is the inferred implicit argument for parameter
cbf
? Where does it come from? - Were there any other implicits which would be also suitable as implicit arguments in the application of
partition2
? If yes, name one and state where is it defined. - How is the type argument for
To
inCanBuildFrom
parameter inferred?
For the questions below we use map
method. For mutable.LinkedList
as well as BitSet
it is initially defined in GenTraversable. It's full signature method is pretty hairy (see scaladoc for details): def map[B, That](f: (A) => B)(implicit bf: CanBuildFrom[GenTraversable[A], B, That]): That
but typically you do not have to worry about it. Notice only the implicit parameter of type CanBuildFrom
as discussed in the question before.
-
What is the type of
a
? What implicit argument has been provided by the compiler? If the final type differs from the original collection one explain why. -
What is the type of
b
? What implicit argument has been provided by the compiler? If the final type differs from the original collection one explain why.