@@ -11,12 +11,11 @@ import scala.reflect.runtime.universe._
1111/**
1212 * A dynamic wrapper for generic values that can be optimized away in reified ASTs, and can verify basic union-style static type constraints.
1313 */
14- class GenericOps [+ A : Generic ](value_ : A , implicitConversions : Any * ) extends Dynamic with NumberOps [A ] {
15- import GenericOps ._
14+ class GenericOps [+ A : Generic ](val rawValue : A , implicitConversions : List [Any ] = Nil ) extends Dynamic with NumberOps [A ] {
1615
17- val value = peel_(value_ )
16+ val value : Any = peel_(rawValue )
1817
19- private lazy val targets_ = value :: implicitConversions.toList ++ (value match {
18+ private lazy val targets_ = value :: implicitConversions ++ (value match {
2019 case (v : java.lang.Byte ) => numOps_(v.byteValue)
2120 case (v : java.lang.Short ) => numOps_(v.shortValue)
2221 case (v : java.lang.Integer ) => numOps_(v.intValue)
@@ -27,66 +26,39 @@ class GenericOps[+A: Generic](value_ : A, implicitConversions: Any*) extends Dyn
2726 case v => Nil
2827 })
2928
30- private [generic] lazy val targetMirrors_ = targets_.map(implementation => {
29+ private lazy val targetMirrors_ = targets_.map(implementation => {
3130 val mirror = runtimeMirror(Thread .currentThread.getContextClassLoader)
3231 mirror.reflect(implementation)(ClassTag (implementation.getClass))
3332 })
3433
35- private [generic] def valueClassName_ = {
34+ private def valueClassName_ = {
3635 if (value == null )
3736 null
3837 else
3938 value.getClass.getName
4039 }
4140
42- def applyDynamic (name : String )(args : Any * ): Any = macro internal.applyDynamic[A ]
43- def selectDynamic (name : String ): Any = macro internal.selectDynamic[A ]
44- def updateDynamic (name : String )(value : Any ): Unit = macro internal.updateDynamic[A ]
45-
46- override def equals (other : Any ) = value.equals(other)
47- override def hashCode () = value.hashCode()
48- override def toString () = " GenericOps(" + value + " )"
49- }
50-
51- trait NumberOps [+ A ] {
52- def + (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
53- def - (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
54- def * (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
55- def / (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
56- def /% (rhs : A ): (A , A ) = macro internal.methodHomogeneous[A , (A , A )]
57- def ==: (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
58- def !=: (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
59- def <= (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
60- def < (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
61- def >= (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
62- def > (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
63- def abs : A = macro internal.method0[A , A ]
64- def signum : Int = macro internal.method0[A , Int ]
65- def toInt : Int = macro internal.method0[A , Int ]
66- def toLong : Long = macro internal.method0[A , Long ]
67- def toFloat : Float = macro internal.method0[A , Float ]
68- def toDouble : Double = macro internal.method0[A , Double ]
69- }
70-
71- object GenericOps {
72-
73- private [generic] def numOps_ [N : math.Numeric ](v : N ): Seq [Any ] = {
41+ private def numOps_ [N : math.Numeric ](v : N ): Seq [Any ] = {
7442 val n = implicitly[math.Numeric [N ]]
7543 List (n.mkNumericOps(v), n.mkOrderingOps(v))
7644 }
7745
78- private [generic] def peel_ (value : Any ): Any = value match {
79- case ops : GenericOps [_] => peel_(ops.value)
80- case _ => value
46+ private def peel_ (value : Any ): Any = {
47+ if (value.isInstanceOf [GenericOps [_]]) {
48+ value.asInstanceOf [GenericOps [_]].value
49+ } else {
50+ value
51+ }
8152 }
8253
83- private def findDecl (ops : GenericOps [_])( name : String ) = {
54+ private def findDecl (name : String ) = {
8455 def sub (mirrors : List [InstanceMirror ]): Option [(Symbol , InstanceMirror )] = mirrors match {
8556 case Nil => None
8657 case mirror :: otherMirrors =>
8758 val tpe = mirror.symbol.asType.toType
8859 val symbol =
89- tpe.member(name : TermName ) orElse tpe.member(reflect.NameTransformer .encode(name): TermName )
60+ tpe.member(name : TermName ) orElse
61+ tpe.member(reflect.NameTransformer .encode(name): TermName )
9062 if (symbol == NoSymbol ) { // } || !(mirror.symbol.asType.toType <:< symbol.owner.asType.toType)) {
9163 // println("No " + name + " or " + reflect.NameTransformer.encode(name) + " in " + mirror.instance.getClass.getName + ": " + tpe + ": " + tpe.members)
9264 sub(otherMirrors)
@@ -96,34 +68,61 @@ object GenericOps {
9668 Some (symbol -> mirror)
9769 }
9870 }
99- sub(ops. targetMirrors_)
71+ sub(targetMirrors_)
10072 }
101- def applyDynamicImpl [ A ]( ops : GenericOps [ A ], name : String , args : Any * ): Any = {
102- findDecl(ops)( name) match {
73+ def applyDynamic ( name : String )( args : Any * ): Any = {
74+ findDecl(name) match {
10375 case Some ((symbol, mirror)) if symbol.isMethod =>
10476 val unwrapped = args.map(peel_(_))
10577 // println("Calling " + name + " on " + mirror.instance)
10678 mirror.reflectMethod(symbol.asMethod)(unwrapped : _* )
10779 case _ =>
108- throw new NoSuchMethodException (" No method '" + name + " ' in " + ops. valueClassName_)
80+ throw new NoSuchMethodException (" No method '" + name + " ' in " + valueClassName_)
10981 }
11082 }
111- def selectDynamicImpl [ A ]( ops : GenericOps [ A ], name : String ): Any = {
112- findDecl(ops)( name) match {
83+ def selectDynamic ( name : String ): Any = {
84+ findDecl(name) match {
11385 case Some ((symbol, mirror)) if symbol.isMethod =>
11486 mirror.reflectMethod(symbol.asMethod)()
11587 case Some ((symbol, mirror)) if symbol.isTerm =>
11688 mirror.reflectField(symbol.asTerm).get
11789 case _ =>
118- throw new NoSuchFieldException (" No field '" + name + " ' in " + ops. valueClassName_)
90+ throw new NoSuchFieldException (" No field '" + name + " ' in " + valueClassName_)
11991 }
12092 }
121- def updateDynamicImpl [ A ]( ops : GenericOps [ A ], name : String , value : Any ) {
122- findDecl(ops)( name) match {
93+ def updateDynamic ( name : String )( value : Any ) {
94+ findDecl(name) match {
12395 case Some ((symbol, mirror)) if symbol.isTerm =>
12496 mirror.reflectField(symbol.asTerm).set(value)
12597 case _ =>
126- throw new NoSuchFieldException (" No field '" + name + " ' in " + ops. valueClassName_)
98+ throw new NoSuchFieldException (" No field '" + name + " ' in " + valueClassName_)
12799 }
128100 }
101+
102+ override def equals (other : Any ) = value.equals(other)
103+ override def hashCode () = value.hashCode()
104+ override def toString () = " GenericOps(" + value + " )"
105+ }
106+
107+ trait NumberOps [+ A ] {
108+ def + (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
109+ def - (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
110+ def * (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
111+ def / (rhs : A ): A = macro internal.methodHomogeneous[A , A ]
112+ def /% (rhs : A ): (A , A ) = macro internal.methodHomogeneous[A , (A , A )]
113+ def ==: (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
114+ def !=: (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
115+ def <= (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
116+ def < (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
117+ def >= (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
118+ def > (rhs : A ): Boolean = macro internal.methodHomogeneous[A , Boolean ]
119+ def abs : A = macro internal.method0[A , A ]
120+ def signum : Int = macro internal.method0[A , Int ]
121+ def toInt : Int = macro internal.method0[A , Int ]
122+ def toLong : Long = macro internal.method0[A , Long ]
123+ def toFloat : Float = macro internal.method0[A , Float ]
124+ def toDouble : Double = macro internal.method0[A , Double ]
125+ }
126+
127+ object GenericOps {
129128}
0 commit comments