Created
March 6, 2021 22:09
-
-
Save deanwampler/6fe5f3540dec4a08049526466f26e53f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from: | |
// https://www.scala-lang.org/2021/02/26/tuples-bring-generic-programming-to-scala-3.html | |
// You might hope the following would work for Tuple.map: | |
scala> (1, 2.2, "three").map(x => Some(x)) | |
1 |(1, 2.2, "three").map(x => Some(x)) | |
| ^^^^^^^^^^^^ | |
| Found: Any => Some[Any] | |
| Required: PolyFunction{apply: [t](x$1: t): Any} | |
// Ah, a PolyFunction is required! We know how to do that... | |
scala> (1, 2.2, "three").map([X] => (x:X) => Some(x)) // Note the additional "[X] =>" | |
val res10: Some[Int] *: Some[Double] *: Some[String] *: | |
scala.Tuple.Map[scala.Tuple$package.EmptyTuple.type, Some] = (Some(1),Some(2.2),Some(three)) | |
// Here it is again with the explicit type parameter passed to map: | |
scala> (1, 2.2, "three").map[[X] =>> Option[X]]([X] => (x:X) => Some(x)) | |
val res9: Option[Int] *: Option[Double] *: Option[String] *: | |
scala.Tuple.Map[scala.Tuple$package.EmptyTuple.type, Option] = (Some(1),Some(2.2),Some(three)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment