Skip to content

Commit ac4d72b

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

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
object Test {
19+
20+
{
21+
given ConversionFunction[Int => String] = (x: Int) => x.toString
22+
// val a: String = 3
23+
val a: String = ConversionFunction.get[3 => String].apply(3)
24+
}
25+
26+
{
27+
given ConversionFunction[(x: Int) => x.type] = ConversionFunction((x: Int) => x)
28+
// val a: String = 3
29+
val a: Int = ConversionFunction.get[3 => Int].apply(3)
30+
}
31+
32+
33+
{
34+
trait X {
35+
type T
36+
def t: T
37+
}
38+
val x: X = ???
39+
40+
given ConversionFunction[(x: X) => x.T] = (x: X) => x.t
41+
// val a: String = 3
42+
val a: x.T = ConversionFunction.get[(x: X) => x.T].apply(x)
43+
}
44+
45+
46+
{
47+
trait X {
48+
type T
49+
def t: T
50+
}
51+
val x: X = ???
52+
53+
given ConversionFunction[(x: X) => x.T] = ConversionFunction(_.t)
54+
// val a: String = 3
55+
val a: x.T = ConversionFunction.get[(x: X) => x.T].apply(x)
56+
}
57+
58+
}

0 commit comments

Comments
 (0)