Skip to content

Commit

Permalink
Show that this solves scala#10929
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Mar 29, 2024
1 parent 3eef2e8 commit 93352e8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/pos/i10929-new-syntax.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//> using options -language:experimental.modularity -source future
trait TupleOf[+A]:
type Self
type Mapped[+A] <: Tuple
def map[B](x: Self)(f: A => B): Mapped[B]

object TupleOf:

given EmptyTuple is TupleOf[Nothing]:
type Mapped[+A] = EmptyTuple
def map[B](x: EmptyTuple)(f: Nothing => B): Mapped[B] = x

given [A, Rest <: Tuple : TupleOf[A]] => A *: Rest is TupleOf[A]:
type Mapped[+A] = A *: Rest.Mapped[A]
def map[B](x: A *: Rest)(f: A => B): Mapped[B] =
(f(x.head) *: Rest.map(x.tail)(f))

def foo[T: TupleOf[Int]](xs: T): T.Mapped[Int] = T.map(xs)(_ + 1)

@main def test =
foo(EmptyTuple): EmptyTuple // ok
foo(1 *: EmptyTuple): Int *: EmptyTuple // now also ok
21 changes: 21 additions & 0 deletions tests/pos/i10929.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//> using options -language:experimental.modularity -source future
infix abstract class TupleOf[T, +A]:
type Mapped[+A] <: Tuple
def map[B](x: T)(f: A => B): Mapped[B]

object TupleOf:

given TupleOf[EmptyTuple, Nothing] with
type Mapped[+A] = EmptyTuple
def map[B](x: EmptyTuple)(f: Nothing => B): Mapped[B] = x

given [A, Rest <: Tuple](using tracked val tup: Rest TupleOf A): TupleOf[A *: Rest, A] with
type Mapped[+A] = A *: tup.Mapped[A]
def map[B](x: A *: Rest)(f: A => B): Mapped[B] =
(f(x.head) *: tup.map(x.tail)(f))

def foo[T](xs: T)(using tup: T TupleOf Int): tup.Mapped[Int] = tup.map(xs)(_ + 1)

@main def test =
foo(EmptyTuple): EmptyTuple // ok
foo(1 *: EmptyTuple): Int *: EmptyTuple // now also ok

0 comments on commit 93352e8

Please sign in to comment.