You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’ve discovered this issue when working on the collections strawman.
importscala.reflect.ClassTag// Base type introducing a flatMap methodtraitColl[A] {
defflatMap[B](f: A=>Coll[B]):Coll[B]
}
// An `Arr[A]` is not a `Coll[A]` ...traitArr[A]
// ... but there is an `ArrOps` decoratortraitArrOps[A] extendsColl[A] {
// we overload `flatMap` for the specific needs of `Arr`defflatMap[B:ClassTag](f: A=>Coll[B]):Arr[B]
}
objectPrelude {
importscala.language.implicitConversionsimplicitdefarrToArrOps[A](array: Arr[A]):ArrOps[A] =???
}
objectUsageextendsApp {
importPrelude._valarray:Arr[Arr[Int]] =???// This line doesn’t compile
array.flatMap(x => x)
// overloaded method value flatMap with alternatives:// [B](f: Arr[Int] => Coll[B])(implicit evidence$1: scala.reflect.ClassTag[B])Arr[B] // [B](f: Arr[Int] => Coll[B])Coll[B]// cannot be applied to (Arr[Int] => Arr[Int])// However if we explicitly trigger the implicit conversion to `Coll`, it compiles:
array.flatMap(x => (x: Coll[Int]))
// Also, if we remove the overload the first one compiles too (even though it requires// two implicit conversions: one to make the `flatMap` method available and one to make// the the result of the function passed to `flatMap` a `Coll`
}
Actually, something similar already works currently with Array and Traversable. I’m wondering if there is a trick to apply to get it work in my case too…
error: overloaded method value foo with alternatives:
(b: () => B)Unit <and>
(a: () => A)Unit
cannot be applied to (() => C)
foo(() => new C) // Fails
^
one error found
The problem in the third case is in inferMethodAlternative: there are no directly applicable alternatives and it fails without considering the possible implicit conversion.
I’ve discovered this issue when working on the collections strawman.
Live snippet: https://scastie.scala-lang.org/uRjdJ280TOqv3az8iGGSOQ
Tested with Scala 2.12.4 and 2.13.0-M2.
The text was updated successfully, but these errors were encountered: