scala - Why is passing Int where an F[_] parameter is expected valid? -
let's have function:
def bar[f[_], a](x: f[a], y: f[a]) = null all following behavior clear:
bar(list(1, 2, 3), list(1)) // compile ok bar(list(1), some(1)) // doesn't compile but,
bar(1, list(1)) // compile ok bar(1, 1) // compile ok why?
p.s. example fsis part 1 - type constructors, functors, , kind projector
i think following gives clue (although there still mystery involved):
def baz[f[_], a](x: f[a]): f[a] = x scala> baz("str") res5: comparable[string] = str scala> baz(1) res6: = 1 scala> class foo defined class foo scala> baz(new foo) <console>:15: error: no type parameters method baz: (x: f[a])f[a] exist can applied arguments (foo) --- because --- argument expression's type not compatible formal parameter type; found : foo required: ?f baz(new foo) ^ <console>:15: error: type mismatch; found : foo required: f[a] baz(new foo) ^ scala> case class foo2 warning: there 1 deprecation warning(s); re-run -deprecation details defined class foo2 scala> baz(foo2) res10: scala.runtime.abstractfunction0[foo2] = foo2 so functions bar , baz finding container type can (comparable string, abstractfunction0 case class, etc) match expected f[_].
speculation: in case of int, suspect hitting special "container" type (boxed) primitive types in underlying bytecode. if special type can printed scala "any", special type can think of "any[_]", explain results see. corroboration special handling of primitives, note fails non-primitive simple types, (non-case) class foo above.
Comments
Post a Comment