Skip to content

Commit aa14d5e

Browse files
author
Oron Port
committed
add value support
1 parent 6a278b7 commit aa14d5e

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/main/scala/singleton/ops/OpIntercept.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import impl._
33

44
import scala.annotation.implicitNotFound
55
@implicitNotFound("Missing an `OpIntercept` implicit for the operation ${Op}")
6-
trait OpIntercept[Op <: HasOut] extends HasOut
6+
trait OpIntercept[Op <: HasOut] extends HasOutValue
77
object OpIntercept {
88
type Aux[Op <: HasOut, Out0] = OpIntercept[Op]{type Out = Out0}
99
}

src/main/scala/singleton/ops/impl/GeneralMacros.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ trait GeneralMacros {
915915
silent = false
916916
)
917917
TypeCalc(itree.tpe.decls.head.info) match {
918-
case t : CalcUnknown => t.copy(opIntercept = true) //the unknown result must be marked properly so we allow it later
918+
case t : CalcUnknown => t.copy(treeOption = Some(c.untypecheck(q"$itree.value")),opIntercept = true) //the unknown result must be marked properly so we allow it later
919919
case t => t
920920
}
921921
} catch {

src/test/scala/singleton/ops/OpInterceptSpec.scala

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,41 @@ import singleton.TestUtils._
66

77
class OpInterceptSpec extends Properties("OpInterceptSpec") {
88

9-
trait Vec[A0, A1]
9+
trait Vec[A0, A1] {
10+
def show(implicit a0 : ValueOf[A0], a1 : ValueOf[A1]) : String = s"Vec[${valueOf[A0]}, ${valueOf[A1]}]"
11+
}
1012

1113
implicit def `Vec+`[VL0, VL1, VR0, VR1](
1214
implicit
1315
opL : VL0 + VR0,
1416
opR : VL1 + VR1
15-
) : OpIntercept.Aux[Vec[VL0, VL1] + Vec[VR0, VR1], Vec[opL.Out, opR.Out]] = ???
17+
) : OpIntercept.Aux[Vec[VL0, VL1] + Vec[VR0, VR1], Vec[opL.Out, opR.Out]] = //Vec is not a singleton value, so we need to instantiate OpIntercept
18+
new OpIntercept[Vec[VL0, VL1] + Vec[VR0, VR1]] {
19+
type Out = Vec[opL.Out, opR.Out]
20+
val value : Out = new Vec[opL.Out, opR.Out]{}
21+
}
1622

1723
implicit def `Vec==`[VL0, VL1, VR0, VR1](
1824
implicit
1925
op : (VL0 == VR0) && (VL1 == VR1)
20-
) : OpIntercept.Aux[Vec[VL0, VL1] == Vec[VR0, VR1], op.Out] = ???
26+
) : OpIntercept.Aux[Vec[VL0, VL1] == Vec[VR0, VR1], op.Out] = ??? //No need to instantiate when a singleton value is returned
2127

22-
property("Custom Vec Equality OK") = wellTyped {
28+
property("Custom Vec Equality OK") = {
2329
val eq1 = shapeless.the[Vec[W.`1`.T, W.`2`.T] == Vec[W.`1`.T, W.`2`.T]]
2430
val eq2 = shapeless.the[Vec[W.`1`.T, W.`2`.T] == Vec[W.`1`.T, W.`1`.T]]
2531
implicitly[eq1.Out =:= W.`true`.T]
2632
implicitly[eq2.Out =:= W.`false`.T]
33+
eq1.value == true
2734
}
2835

29-
property("Custom Vec Addition OK") = wellTyped {
36+
property("Custom Vec Addition OK") = {
3037
val add2 = shapeless.the[Vec[W.`1`.T, W.`2`.T] + Vec[W.`3`.T, W.`8`.T]]
3138
val add3 = shapeless.the[Vec[W.`1`.T, W.`2`.T] + Vec[W.`3`.T, W.`8`.T] + Vec[W.`20`.T, W.`20`.T]]
3239
implicitly[add2.Out =:= Vec[W.`4`.T, W.`10`.T]]
3340
implicitly[add3.Out =:= Vec[W.`24`.T, W.`30`.T]]
3441
val add23 = shapeless.the[add2.Out + add3.Out]
3542
implicitly[add23.Out =:= Vec[W.`28`.T, W.`40`.T]]
43+
add2.value.show == "Vec[4, 10]"
3644
}
3745

3846

@@ -41,13 +49,14 @@ class OpInterceptSpec extends Properties("OpInterceptSpec") {
4149
implicit def doFib[P](
4250
implicit
4351
op : ITE[P == W.`0`.T, W.`0`.T, ITE[P == W.`1`.T, W.`1`.T, Fib[P - W.`1`.T] + Fib[P - W.`2`.T]]]
44-
) : OpIntercept.Aux[Fib[P], op.Out] = ???
52+
) : OpIntercept.Aux[Fib[P], op.Out] = ??? //No need to instantiate when a singleton value is returned
4553

46-
property("Custom Fibonacci Op OK") = wellTyped {
54+
property("Custom Fibonacci Op OK") = {
4755
val fib4 = shapeless.the[Fib[W.`4`.T]]
4856
implicitly[fib4.Out =:= W.`3`.T]
4957
val fib10 = shapeless.the[Fib[W.`10`.T]]
5058
implicitly[fib10.Out =:= W.`55`.T]
59+
fib10.value == 55
5160
}
5261

5362

0 commit comments

Comments
 (0)