forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |