| layout | page |
|---|---|
| title | 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
Tforfooinferred: a) at assignment tores1b) at applicationres1(2)? -
What is the type of the expression
res1? -
Why does the typechecker infer
Nothingas type argument forT? -
Why does the typechecker not infer
Anyinstead ? -
foois 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
Twhile inferring type argument forbar? -
Using your answer to the previous question explain why is the inferred type argument for
Tnow 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
fooapplication, 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
Uwhile applyingfootonew Base? -
What constraint(s) will the typechecker use in order to infer type argument
Twhile applyingfootonew Base? -
Why is the inferred type argument for
TNothing? -
What constraint(s) will the typechecker use in order to infer type argument
Twhile applyingbartonew Base? -
What constraint(s) will the typechecker use in order to infer type constructor
Uwhile applyingbartonew Base? -
Why is the compiler able to infer acceptable type arguments for
TandUbut 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
p1andp2. Where is the membertakeWhileinitially defined ? Why is the type ofp1inferred 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
TandCC? - We do not provide the implicit argument for
cbfdirectly 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
ToinCanBuildFromparameter 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.