Skip to content

Commit c5399fd

Browse files
committed
Add prototype conversion with dependent functions support
1 parent a2354a8 commit c5399fd

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package scala
2+
3+
4+
object ConversionFunction {
5+
6+
opaque type ConversionFunction[+F <: Nothing => Any] = F
7+
8+
def apply[F <: Nothing => Any](f: F): ConversionFunction[F] = f
9+
def get[F <: Nothing => Any](using f: ConversionFunction[F]): F = f
10+
11+
}
12+
13+
given [F <: Nothing => Any] as Conversion/*Function*/[F, ConversionFunction[F]] = (x: F) => ConversionFunction.apply(x)
14+
15+
type ConversionFunction[+F <: Nothing => Any] =
16+
ConversionFunction.ConversionFunction[F]
17+
18+
type Conversion2[T, U] =
19+
ConversionFunction.ConversionFunction[T => U]
20+
21+
object Test {
22+
23+
{
24+
given Conversion2[Int, String] = (x: Int) => x.toString
25+
// val a: String = 3
26+
val a: String = ConversionFunction.get[3 => String].apply(3)
27+
}
28+
29+
{
30+
given ConversionFunction[Int => String] = (x: Int) => x.toString
31+
// val a: String = 3
32+
val a: String = ConversionFunction.get[3 => String].apply(3)
33+
}
34+
35+
{
36+
given ConversionFunction[(x: Int) => x.type] = ConversionFunction((x: Int) => x)
37+
// val a: String = 3
38+
val a: Int = ConversionFunction.get[3 => Int].apply(3)
39+
}
40+
41+
42+
{
43+
trait X {
44+
type T
45+
def t: T
46+
}
47+
val x: X = ???
48+
49+
given ConversionFunction[(x: X) => x.T] = (x: X) => x.t
50+
// val a: String = 3
51+
val a: x.T = ConversionFunction.get[(x: X) => x.T].apply(x)
52+
}
53+
54+
55+
{
56+
trait X {
57+
type T
58+
def t: T
59+
}
60+
val x: X = ???
61+
62+
given ConversionFunction[(x: X) => x.T] = ConversionFunction(_.t)
63+
// val a: String = 3
64+
val a: x.T = ConversionFunction.get[(x: X) => x.T].apply(x)
65+
}
66+
67+
}

0 commit comments

Comments
 (0)