Skip to content

Commit

Permalink
Generalize quantification of measures. Remove redundant code.
Browse files Browse the repository at this point in the history
  • Loading branch information
neuralagent committed Jan 7, 2017
1 parent 5837443 commit 70bd28f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 102 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ private[measure] object QuantifyMeasure

val targetTraitTpe: Type = c.typecheck(q"0.asInstanceOf[$targetTraitTree]").tpe

val quantityTree: Tree = c.prefix.tree match
{
case q"new $name[$targetTraitTree, $quantityTree](...$paramss)" => quantityTree
}

val isAnyMeasure = quantityTree match
{
case tq"Quantity[$numericType, AnyMeasure]" => true
case _ => false
}

val measuresScope: List[Tree] = c.prefix.tree match
{
case q"new $name[$targetTraitTpe, $quantityTpe](...$paramss)" if paramss.nonEmpty => paramss.head
Expand All @@ -59,7 +70,11 @@ private[measure] object QuantifyMeasure
val quantityIdentifier = TermName(measureValTermSymbol.name.toString.trim)
val measureTermName = TermName(measureValTermSymbol.name.toString.trim)

val quantityTpeIdentifier = q"""Quantity[Double, ${measureValTermSymbol.typeSignature}]"""
val quantityTpeIdentifier =
if (isAnyMeasure)
q"""Quantity[Double, AnyMeasure]"""
else
q"""Quantity[Double, ${measureValTermSymbol.typeSignature}]"""

q"""def $quantityIdentifier = $quantityTpeIdentifier(value, ${measuresScope.head}.$measureTermName)"""
})
Expand All @@ -79,8 +94,27 @@ private[measure] object QuantifyMeasure
case _ => c.abort(c.enclosingPosition, s"QuantifyMeasure annotation can only be used with classes.")
}

c.Expr(
q"""
if (isAnyMeasure)
{
c.Expr(
q"""
implicit class $className(val value: Double) extends AnyVal
{
implicit def qn: QuasiNumeric[Double] = implicitly(QuasiNumeric.doubleQuasiNumeric)

def apply(measure: AnyMeasure): Quantity[Double, AnyMeasure] = Quantity(value, measure)

def *(measure: AnyMeasure): Quantity[Double, AnyMeasure] = apply(measure)

..$quantityDefs
}
"""
)
}
else
{
c.Expr(
q"""
implicit class $className(val value: Double) extends AnyVal
{
implicit def qn: QuasiNumeric[Double] = implicitly(QuasiNumeric.doubleQuasiNumeric)
Expand All @@ -92,6 +126,7 @@ private[measure] object QuantifyMeasure
..$quantityDefs
}
"""
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ package object measure extends DefaultDimensions
object any
{

@QuantifyAnyMeasure[DefaultMeasures, Quantity[Double, AnyMeasure]](measuresScope)
@QuantifyMeasure[DefaultMeasures, Quantity[Double, AnyMeasure]](measuresScope)
class QuantifiedAnyMeasures(val value: Double)

}
Expand Down

0 comments on commit 70bd28f

Please sign in to comment.