Re: Sammy + wildcards + type inference = :(
Adriaan Moors <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CA+cgcwZEfdyEu0Bwxn=2FFWYUhNo4PWVv_zP3ki5-Oy7OtjJxw@mail.gmail.com> |
Hi, Yes, I'm still working on having type inference understand Java's use-site variance in terms of Scala's definition-site variance. (Here's a good paper <http://yanniss.github.io/varj-ecoop12.pdf> on the topic -- it's tricky :-)) Meanwhile, specifying the type arguments works around this limitation: scala> new java.util.ArrayList[String]().stream.map[Int](_.toInt) res2: java.util.stream.Stream[Int] = java.util.stream.ReferencePipeline$3@28dcca0c scala> new java.util.ArrayList[String]().stream.map[Int](_.toInt).map[String](_.toString) res3: java.util.stream.Stream[String] = java.util.stream.ReferencePipeline$3@6f7923a5 cheers adriaan On Fri, Jun 26, 2015 at 6:40 PM, Roman Janusz <[email protected]> wrote: > Not much of an improvement if I can't chain: > > scala> new > java.util.ArrayList[String]().stream.map(_.toInt).map(_.toString): > JStream[String] > <console>:9: error: no type parameters for method map: (x$1: > java.util.function.Function[_ >: String, _ <: R])java.util.stream.Stream[R] > exist so that it can be applied to arguments > (java.util.function.Function[String,Int] with Serializable) > --- because --- > argument expression's type is not compatible with formal parameter type; > found : java.util.function.Function[String,Int] with Serializable > required: java.util.function.Function[_ >: String, _ <: ?R] > Note: String <: Any (and java.util.function.Function[String,Int] with > Serializable <: java.util.function.Function[String,Int]), but Java-defined > trait Function is invariant in type T. > You may wish to investigate a wildcard type such as `_ <: Any`. (SLS > 3.2.10) > new > java.util.ArrayList[String]().stream.map(_.toInt).map(_.toString): > JStream[String] > ^ > <console>:9: error: type mismatch; > found : java.util.function.Function[String,Int] with Serializable > required: java.util.function.Function[_ >: String, _ <: R] > new > java.util.ArrayList[String]().stream.map(_.toInt).map(_.toString): > JStream[String] > > > W dniu sobota, 27 czerwca 2015 03:37:48 UTC+2 użytkownik Stephen Compall > napisał: >> >> On Fri, 2015-06-26 at 18:05 -0700, Roman Janusz wrote: >> >> Is there any chance this will work? >> >> $ scala -Xexperimental >> Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java >> 1.8.0_40). >> Type in expressions to have them evaluated. >> Type :help for more information. >> >> scala> new java.util.ArrayList[String]().stream.map(_.toInt) >> <console>:8: error: no type parameters for method map: (x$1: java.util. >> function.Function[_ >: String, _ <: R])java.util.stream.Stream[R] exist >> so that it can be applied to arguments (java.util.function.Function[ >> String,Int] with Serializable) >> --- because --- >> argument expression's type is not compatible with formal parameter type; >> found : java.util.function.Function[String,Int] with Serializable >> required: java.util.function.Function[_ >: String, _ <: ?R] >> >> >> Not fatal, I would say. >> >> Welcome to Scala version 2.11.7 (OpenJDK 64-Bit Server VM, Java 1.8.0_45). >> Type in expressions to have them evaluated. >> Type :help for more information. >> >> scala> import java.util.ArrayList, java.util.stream.{Stream => JStream} >> import java.util.ArrayList >> import java.util.stream.{Stream=>JStream} >> >> scala> new java.util.ArrayList[String]().stream.map[Int](_.toInt) >> res1: java.util.stream.Stream[Int] = java.util.stream.ReferencePipeline$3...@5b49c202 >> >> scala> new java.util.ArrayList[String]().stream.map(_.toInt): JStream[Int] >> res2: java.util.stream.Stream[Int] = java.util.stream.ReferencePipeline$3...@47bded3c >> >> >> Not ideal, but maybe something else is going on here. >> >> -- >> Stephen Compall >> "^aCollection allSatisfy: [:each|aCondition]": less is better >> >> -- > You received this message because you are subscribed to the Google Groups > "scala-language" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "scala-language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.