Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Try other Tuple map method #19600

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sealed trait Tuple extends Product {
* If the tuple is of the form `a1 *: ... *: Tuple` (that is, the tail is not known
* to be the cons type.
*/
inline def map[F[_]](f: [t] => t => F[t]): Map[this.type, F] =
inline def map[F[_ <: Union[this.type]]](f: (t: Union[this.type]) => F[t.type]): Map[this.type, F] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break TASTy compatibility. We can avoid this breakage with the trick in #19185.

runtime.Tuples.map(this, f).asInstanceOf[Map[this.type, F]]

/** Given a tuple `(a1, ..., am)`, returns the tuple `(a1, ..., an)` consisting
Expand Down
6 changes: 4 additions & 2 deletions library/src/scala/runtime/Tuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,11 @@ object Tuples {
)
}

def map[F[_]](self: Tuple, f: [t] => t => F[t]): Tuple = self match {
def map[T <: Tuple, F[_ <: Tuple.Union[T]]](self: T, f: (t: Tuple.Union[T]) => F[t.type]): Tuple = self match {
case EmptyTuple => self
case _ => fromIArray(self.productIterator.map(f(_).asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) // TODO use toIArray
case _ =>
val iterator = self.productIterator.asInstanceOf[Iterator[Tuple.Union[T]]]
fromIArray(iterator.map(f(_)).toArray.asInstanceOf[IArray[Object]]) // TODO use toIArray
}

def take(self: Tuple, n: Int): Tuple = {
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i14351.scala
Original file line number Diff line number Diff line change
@@ -1 +1 @@
val p: (Option[Int], Option[String]) = (1,"foo").map([T] => (x: T) => Option.apply[T](x))
val p: (Option[Int], Option[String]) = (1,"foo").map(Option(_))
9 changes: 2 additions & 7 deletions tests/pos/i8300.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@ type Bar[X] = X match {
}

object Test:
(Set(1, 2, 3), List("a", "b")).map(
[A] =>
(a: A) =>
a match {
case it: Iterable[x] => it.map(Tuple1(_)).asInstanceOf[Bar[A]]
}
)
(Set(1, 2, 3), List("a", "b")).map:
a => a.map(Tuple1(_)).asInstanceOf[Bar[a.type]]
27 changes: 24 additions & 3 deletions tests/run/tuple-map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ object Test extends App {
val tuple: Tuple = ("1", "2", "3", "4", "5")
val tupleXXL: Tuple = ("11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35")

type Id[X] = X
val f: [t] => t => Id[t] = [t] => (x: t) => {
type F[X] = String
val f: (t: Any) => F[t.type] = x => {
val str = x.asInstanceOf[String]
str.updated(0, (str(0) + 1).toChar).asInstanceOf[t]
str.updated(0, (str(0) + 1).toChar)
}

// Test all possible combinations of making
println(emptyTuple.map(f))
println(tuple.map(f))
println(tupleXXL.map(f))

// NOTE F is needed in ascription of f above for inference of .map
def alternative: Unit =
val f: (t: Any) => String = x => {
val str = x.asInstanceOf[String]
str.updated(0, (str(0) + 1).toChar)
}
println(emptyTuple.map[F](f))
println(tuple.map[F](f))
println(tupleXXL.map[F](f))

// Given the body of f, the following is more likely to occur in practice:
def withoutWidening: Unit =
val emptyTuple = Tuple()
val tuple = ("1", "2", "3", "4", "5")
val tupleXXL = ("11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35")
val f: (s: String) => F[s.type] = str => str.updated(0, (str(0) + 1).toChar)
println(emptyTuple.map(f))
println(tuple.map(f))
println(tupleXXL.map(f))

}
8 changes: 3 additions & 5 deletions tests/run/tuple-ops.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ val r5: Unit = a.zip(d)
// Map
case class Foo[X](x: X)

val r6: (Int, Int, Int) = a.map[[t] =>> Int]([t] => (x: t) => x match {
case x: Int => x * x
case _ => ???
})
val r6: (Int, Int, Int) = a.map[[t] =>> Int](x => x * x)

val r7: ((1, Foo[1]), (2, Foo[2]), (3, Foo[3])) =
a.map[[t] =>> (t, Foo[t])]( [t] => (x: t) => (x, Foo(x)) )
a.map[[t] =>> (t, Foo[t])](x => (x, Foo(x)))
// NOTE might not need give type param if had `precise` modifier for x

// More Zip
val t1: Int *: Long *: Tuple = (1, 2l, 100, 200)
Expand Down
Loading