Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Created March 6, 2021 22:09
Show Gist options
  • Save deanwampler/6fe5f3540dec4a08049526466f26e53f to your computer and use it in GitHub Desktop.
Save deanwampler/6fe5f3540dec4a08049526466f26e53f to your computer and use it in GitHub Desktop.
// 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