Skip to content

Commit

Permalink
add some syntax options
Browse files Browse the repository at this point in the history
  • Loading branch information
erikerlandson committed Feb 24, 2023
1 parent a827c9f commit 475db4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
35 changes: 23 additions & 12 deletions runtime/src/main/scala/coulomb/runtime/runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ object RuntimeUnit:
case class Pow(b: RuntimeUnit, e: Rational) extends RuntimeUnit
inline def of[U]: RuntimeUnit = ${ infra.runtime.meta.unitRTU[U] }

def runtimeCoefficient[V](uf: RuntimeUnit, ut: RuntimeUnit)(using
crt: CoefficientRuntime,
vc: ValueConversion[Rational, V]
): Either[String, V] =
crt.coefficient[V](uf, ut)

package syntax {
extension [V](v: V)
inline def withRuntimeUnit(u: RuntimeUnit): RuntimeQuantity[V] =
RuntimeQuantity(v, u)

inline def withRuntimeUnit[U]: RuntimeQuantity[V] =
RuntimeQuantity(v, RuntimeUnit.of[U])
}

case class RuntimeQuantity[V](value: V, unit: RuntimeUnit)

object RuntimeQuantity:
Expand All @@ -58,6 +73,14 @@ object RuntimeQuantity:
inline def apply[V, U](q: Quantity[V, U]): RuntimeQuantity[V] =
RuntimeQuantity(q.value, RuntimeUnit.of[U])

inline def apply[U](using a: Applier[U]) = a

class Applier[U]:
inline def apply[V](v: V): RuntimeQuantity[V] =
RuntimeQuantity(v, RuntimeUnit.of[U])
object Applier:
given ctx_Applier[U]: Applier[U] = new Applier[U]

extension [VL](ql: RuntimeQuantity[VL])
inline def toQuantity[VR, UR](using
crt: CoefficientRuntime,
Expand All @@ -72,18 +95,6 @@ object RuntimeQuantity:
add: RuntimeAdd[VL, VR]
): Either[String, RuntimeQuantity[add.VO]] = add.eval(ql, qr)

package syntax {
extension [V](v: V)
inline def withRuntimeUnit(u: RuntimeUnit): RuntimeQuantity[V] =
RuntimeQuantity(v, u)
}

def runtimeCoefficient[V](uf: RuntimeUnit, ut: RuntimeUnit)(using
crt: CoefficientRuntime,
vc: ValueConversion[Rational, V]
): Either[String, V] =
crt.coefficient[V](uf, ut)

abstract class CoefficientRuntime:
def coefficientRational(
uf: RuntimeUnit,
Expand Down
19 changes: 7 additions & 12 deletions runtime/src/test/scala/coulomb/runtimequantity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,22 @@ abstract class RuntimeQuantitySuite(using CoefficientRuntime)
import coulomb.policy.strict.given
import coulomb.policy.overlay.runtime.strict.given

(RuntimeQuantity(1d, RuntimeUnit.of[Meter])
+ RuntimeQuantity(1d, RuntimeUnit.of[Kilo * Meter]))
.assertR(RuntimeQuantity(1001d, RuntimeUnit.of[Meter]))
(1d.withRuntimeUnit[Meter] + 1d.withRuntimeUnit[Kilo * Meter])
.assertR(RuntimeQuantity[Meter](1001d))

(RuntimeQuantity(1d, RuntimeUnit.of[Second])
+ RuntimeQuantity(1d, RuntimeUnit.of[Kilo * Meter])).assertL
(1d.withRuntimeUnit[Second] + 1d.withRuntimeUnit[Kilo * Meter]).assertL

assertCE("""
(RuntimeQuantity(1, RuntimeUnit.of[Meter])
+ RuntimeQuantity(1d, RuntimeUnit.of[Kilo * Meter]))
(1.withRuntimeUnit[Meter] + 1d.withRuntimeUnit[Kilo * Meter])
""")
}

test("addition standard") {
import coulomb.policy.standard.given
import coulomb.policy.overlay.runtime.standard.given

(RuntimeQuantity(1, RuntimeUnit.of[Meter])
+ RuntimeQuantity(1d, RuntimeUnit.of[Kilo * Meter]))
.assertR(RuntimeQuantity(1001d, RuntimeUnit.of[Meter]))
(1.withRuntimeUnit[Meter] + 1d.withRuntimeUnit[Kilo * Meter])
.assertR(RuntimeQuantity[Meter](1001d))

(RuntimeQuantity(1, RuntimeUnit.of[Second])
+ RuntimeQuantity(1d, RuntimeUnit.of[Kilo * Meter])).assertL
(1.withRuntimeUnit[Second] + 1d.withRuntimeUnit[Kilo * Meter]).assertL
}

0 comments on commit 475db4e

Please sign in to comment.