Skip to content

Commit

Permalink
scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
erikerlandson committed Apr 18, 2024
1 parent 65d2864 commit 2369fee
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 78 deletions.
35 changes: 24 additions & 11 deletions core/src/main/scala/coulomb/conversion/coefficient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package coulomb.conversion
import scala.annotation.implicitNotFound

@implicitNotFound(
"No coefficient could be derived for value type ${V}, unit types ${UF} => ${UT}"
"No coefficient could be derived for value type ${V}, unit types ${UF} => ${UT}"
)
class Coefficient[V, UF, UT](val value: V)

Expand Down Expand Up @@ -53,22 +53,35 @@ object Coefficient:
case _: java.lang.Double =>
coefficientDoubleJ[UF, UT].asInstanceOf[V]
case _ =>
summonInline[Coefficient[V, UF, UT]].value
summonInline[Coefficient[V, UF, UT]].value

inline given g_Coefficient[V, UF, UT]: Coefficient[V, UF, UT] =
inline erasedValue[V] match
case _: Float =>
new Coefficient[V, UF, UT](coefficientFloat[UF, UT].asInstanceOf[V])
case _: Double =>
new Coefficient[V, UF, UT](coefficientDouble[UF, UT].asInstanceOf[V])
case _: Float =>
new Coefficient[V, UF, UT](
coefficientFloat[UF, UT].asInstanceOf[V]
)
case _: Double =>
new Coefficient[V, UF, UT](
coefficientDouble[UF, UT].asInstanceOf[V]
)
case _: BigDecimal =>
new Coefficient[V, UF, UT](coefficientBigDecimal[UF, UT].asInstanceOf[V])
new Coefficient[V, UF, UT](
coefficientBigDecimal[UF, UT].asInstanceOf[V]
)
case _: Rational =>
new Coefficient[V, UF, UT](coefficientRational[UF, UT].asInstanceOf[V])
new Coefficient[V, UF, UT](
coefficientRational[UF, UT].asInstanceOf[V]
)
case _: java.lang.Float =>
new Coefficient[V, UF, UT](coefficientFloatJ[UF, UT].asInstanceOf[V])
new Coefficient[V, UF, UT](
coefficientFloatJ[UF, UT].asInstanceOf[V]
)
case _: java.lang.Double =>
new Coefficient[V, UF, UT](coefficientDoubleJ[UF, UT].asInstanceOf[V])
new Coefficient[V, UF, UT](
coefficientDoubleJ[UF, UT].asInstanceOf[V]
)
case _ =>
val (_, vc) = summonAll[(Fractional[V], ValueConversion[Rational, V])]
val (_, vc) =
summonAll[(Fractional[V], ValueConversion[Rational, V])]
new Coefficient[V, UF, UT](vc(coefficientRational[UF, UT]))
31 changes: 16 additions & 15 deletions core/src/main/scala/coulomb/conversion/deltaunit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,22 @@ object DeltaUnitConversion:

inline def apply[V, B, UF, UT](v: V): V =
if (typeexpr.teq[UF, UT]) v
else inline erasedValue[V] match
case _: Float =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: Double =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: BigDecimal =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: Rational =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: java.lang.Float =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: java.lang.Double =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _ =>
summonInline[DeltaUnitConversion[V, B, UF, UT]](v)
else
inline erasedValue[V] match
case _: Float =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: Double =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: BigDecimal =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: Rational =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: java.lang.Float =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _: java.lang.Double =>
applyShim[V, B, UF, UT](v)(using summonInline[Rng[V]])
case _ =>
summonInline[DeltaUnitConversion[V, B, UF, UT]](v)

inline given g_DeltaUnitConversion[V, B, UF, UT](using
Rng[V]
Expand Down
23 changes: 12 additions & 11 deletions core/src/main/scala/coulomb/conversion/offset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package coulomb.conversion
import scala.annotation.implicitNotFound

@implicitNotFound(
"No offset could be derived for Offset[${V}, ${U}, ${B}]"
"No offset could be derived for Offset[${V}, ${U}, ${B}]"
)
class Offset[V, U, B](val value: V)

Expand All @@ -32,29 +32,30 @@ object Offset:

inline def apply[V, U, B]: V =
inline erasedValue[V] match
case _: Float => deltaOffsetFloat[U, B].asInstanceOf[V]
case _: Double => deltaOffsetDouble[U, B].asInstanceOf[V]
case _: BigDecimal => deltaOffsetBigDecimal[U, B].asInstanceOf[V]
case _: Rational => deltaOffsetRational[U, B].asInstanceOf[V]
case _: java.lang.Float => deltaOffsetFloatJ[U, B].asInstanceOf[V]
case _: Float => deltaOffsetFloat[U, B].asInstanceOf[V]
case _: Double => deltaOffsetDouble[U, B].asInstanceOf[V]
case _: BigDecimal => deltaOffsetBigDecimal[U, B].asInstanceOf[V]
case _: Rational => deltaOffsetRational[U, B].asInstanceOf[V]
case _: java.lang.Float => deltaOffsetFloatJ[U, B].asInstanceOf[V]
case _: java.lang.Double => deltaOffsetDoubleJ[U, B].asInstanceOf[V]
case _ =>
summonInline[Offset[V, U, B]].value

inline given g_Offset[V, U, B]: Offset[V, U, B] =
inline erasedValue[V] match
case _: Float =>
case _: Float =>
new Offset[V, U, B](deltaOffsetFloat[U, B].asInstanceOf[V])
case _: Double =>
case _: Double =>
new Offset[V, U, B](deltaOffsetDouble[U, B].asInstanceOf[V])
case _: BigDecimal =>
new Offset[V, U, B](deltaOffsetBigDecimal[U, B].asInstanceOf[V])
case _: Rational =>
new Offset[V, U, B](deltaOffsetRational[U, B].asInstanceOf[V])
case _: java.lang.Float =>
case _: java.lang.Float =>
new Offset[V, U, B](deltaOffsetFloatJ[U, B].asInstanceOf[V])
case _: java.lang.Double =>
case _: java.lang.Double =>
new Offset[V, U, B](deltaOffsetDoubleJ[U, B].asInstanceOf[V])
case _ =>
val (_, vc) = summonAll[(Fractional[V], ValueConversion[Rational, V])]
val (_, vc) =
summonAll[(Fractional[V], ValueConversion[Rational, V])]
new Offset[V, U, B](vc(deltaOffsetRational[U, B]))
47 changes: 30 additions & 17 deletions core/src/main/scala/coulomb/conversion/unit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,36 @@ object UnitConversion:

inline def apply[V, UF, UT](v: V): V =
if (typeexpr.teq[UF, UT]) v
else inline erasedValue[V] match
// This enumerates all the cases where I have an inline macro for the coefficient
case _: Float =>
applyShim[V, UF, UT](v)(using summonInline[MultiplicativeSemigroup[V]])
case _: Double =>
applyShim[V, UF, UT](v)(using summonInline[MultiplicativeSemigroup[V]])
case _: BigDecimal =>
applyShim[V, UF, UT](v)(using summonInline[MultiplicativeSemigroup[V]])
case _: Rational =>
applyShim[V, UF, UT](v)(using summonInline[MultiplicativeSemigroup[V]])
case _: java.lang.Float =>
applyShim[V, UF, UT](v)(using summonInline[MultiplicativeSemigroup[V]])
case _: java.lang.Double =>
applyShim[V, UF, UT](v)(using summonInline[MultiplicativeSemigroup[V]])
case _ =>
// otherwise summon a unit conversion from context
summonInline[UnitConversion[V, UF, UT]](v)
else
inline erasedValue[V] match
// This enumerates all the cases where I have an inline macro for the coefficient
case _: Float =>
applyShim[V, UF, UT](v)(using
summonInline[MultiplicativeSemigroup[V]]
)
case _: Double =>
applyShim[V, UF, UT](v)(using
summonInline[MultiplicativeSemigroup[V]]
)
case _: BigDecimal =>
applyShim[V, UF, UT](v)(using
summonInline[MultiplicativeSemigroup[V]]
)
case _: Rational =>
applyShim[V, UF, UT](v)(using
summonInline[MultiplicativeSemigroup[V]]
)
case _: java.lang.Float =>
applyShim[V, UF, UT](v)(using
summonInline[MultiplicativeSemigroup[V]]
)
case _: java.lang.Double =>
applyShim[V, UF, UT](v)(using
summonInline[MultiplicativeSemigroup[V]]
)
case _ =>
// otherwise summon a unit conversion from context
summonInline[UnitConversion[V, UF, UT]](v)

inline given g_UnitConversion[V, UF, UT](using
MultiplicativeSemigroup[V]
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/scala/coulomb/infra/typeexpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ object typeexpr:

inline def teq[T1, T2]: Boolean = ${ teqImpl[T1, T2] }

private def teqImpl[T1, T2](using Quotes, Type[T1], Type[T2]): Expr[Boolean] =
private def teqImpl[T1, T2](using
Quotes,
Type[T1],
Type[T2]
): Expr[Boolean] =
import quotes.reflect.*
if (TypeRepr.of[T1] =:= TypeRepr.of[T2])
'{ true }
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/coulomb/policy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package coulomb.policy
* }}}
*/
object standard:
//export coulomb.conversion.standard.scala.given
// export coulomb.conversion.standard.scala.given
export coulomb.cats.all.given
export coulomb.conversion.ops.policy.given

Expand Down
6 changes: 2 additions & 4 deletions core/src/main/scala/coulomb/quantity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package coulomb


/**
* Represents the product of two unit expressions
* @tparam L
Expand Down Expand Up @@ -98,7 +97,7 @@ object Quantity:
object Applier:
given g_Applier[U]: Applier[U] = new Applier[U]

extension[V](v: V)
extension [V](v: V)
/**
* Lift a raw value into a unit quantity
* @tparam U
Expand All @@ -111,7 +110,7 @@ object Quantity:
*/
inline def withUnit[U]: Quantity[V, U] = v

extension[V, U, Q[V, U] <: Quantity[V, U]](q: Q[V, U])
extension [V, U, Q[V, U] <: Quantity[V, U]](q: Q[V, U])
/**
* extract the raw value of a unit quantity
* @return
Expand Down Expand Up @@ -545,4 +544,3 @@ object foo:
import coulomb.conversion.ops.policy.given
import coulomb.policy.algebras.given
val x = 1d.withUnit[1] + 1d.withUnit[1000]

4 changes: 2 additions & 2 deletions core/src/main/scala/coulomb/syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export coulomb.DeltaQuantity.withDeltaUnit
// there are three tricks I applied to get scalar factors to work.
// 1. I use the signature:
// extension[V, U, Q[V, U] <: Quantity[V, U]](q: Q[V, U])
// for the Quantity methods, which helps the type system to
// for the Quantity methods, which helps the type system to
// distinguish from the left-factor overloadings defined in this file.
// 2. I define the right-factor overloadings in the Quantity extension,
// because defining them separately here is confusing the compiler
// 3. I curry `using alg: ...` first below, which allows the compiler to
// pick the correct typeclass.

extension[V](v: V)
extension [V](v: V)
inline def *[U](using
alg: MultiplicativeSemigroup[V]
)(q: Quantity[V, U]): Quantity[V, U] =
Expand Down
10 changes: 5 additions & 5 deletions core/src/test/scala/coulomb/quantity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,13 @@ class QuantitySuite extends CoulombSuite:
}

test("scalar factors") {
val q = 2d.withUnit[Meter]
val q = 2d.withUnit[Meter]

(2d * q).assertQ[Double, Meter](4)
(2d / q).assertQ[Double, 1 / Meter](1)
(2d * q).assertQ[Double, Meter](4)
(2d / q).assertQ[Double, 1 / Meter](1)

(q * 2d).assertQ[Double, Meter](4)
(q / 2d).assertQ[Double, Meter](1)
(q * 2d).assertQ[Double, Meter](4)
(q / 2d).assertQ[Double, Meter](1)
}

test("cats Eq, Ord, Hash") {
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/scala/coulomb/testing/units.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ final type Kelvin
given unit_Kelvin: BaseUnit[Kelvin, "kelvin", "K"] = BaseUnit()

final type Celsius
given unit_Celsius
: DeltaUnit[Celsius, Kelvin, 27315 / 100, "celsius", "°C"] = DeltaUnit()
given unit_Celsius: DeltaUnit[Celsius, Kelvin, 27315 / 100, "celsius", "°C"] =
DeltaUnit()

final type Fahrenheit
given unit_Fahrenheit
Expand Down
8 changes: 7 additions & 1 deletion pureconfig/src/main/scala/coulomb/io.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@ object quantity:
// (or equivalent) to the unit we are loading from
inline given ctx_Quantity_Reader_NoVC[V, U](using
nocv: NotGiven[
GivenAll[(Fractional[V], ValueConversion[Rational, V], MultiplicativeSemigroup[V])]
GivenAll[
(
Fractional[V],
ValueConversion[Rational, V],
MultiplicativeSemigroup[V]
)
]
],
crq: ConfigReader[RuntimeQuantity[V]],
crt: CoefficientRuntime
Expand Down
8 changes: 4 additions & 4 deletions units/src/main/scala/coulomb/units/mksa.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object mksa:

/** Unit of electric charge */
final type Coulomb
given unit_Coulomb
: DerivedUnit[Coulomb, Ampere * Second, "coulomb", "C"] = DerivedUnit()
given unit_Coulomb: DerivedUnit[Coulomb, Ampere * Second, "coulomb", "C"] =
DerivedUnit()

/** Unit of electric voltage */
final type Volt
Expand Down Expand Up @@ -64,8 +64,8 @@ object mksa:

/** Unit of magnetic flux density */
final type Tesla
given unit_Tesla
: DerivedUnit[Tesla, Weber / (Meter ^ 2), "tesla", "T"] = DerivedUnit()
given unit_Tesla: DerivedUnit[Tesla, Weber / (Meter ^ 2), "tesla", "T"] =
DerivedUnit()

/** Unit of electric inductance */
final type Henry
Expand Down
3 changes: 2 additions & 1 deletion units/src/main/scala/coulomb/units/temperature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ object temperature:
* val freeze = (0.0).withTemperature[Celsius]
* }}}
*/
inline def withTemperature[U]: Temperature[V, U] = v.withDeltaUnit[U, Kelvin]
inline def withTemperature[U]: Temperature[V, U] =
v.withDeltaUnit[U, Kelvin]
3 changes: 1 addition & 2 deletions units/src/main/scala/coulomb/units/us.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ object us:
* - https://en.wikipedia.org/wiki/International_yard_and_pound
*/
final type Yard
given unit_Yard
: DerivedUnit[Yard, (9144 / 10000) * Meter, "yard", "yd"] =
given unit_Yard: DerivedUnit[Yard, (9144 / 10000) * Meter, "yard", "yd"] =
DerivedUnit()

/**
Expand Down
3 changes: 2 additions & 1 deletion units/src/test/scala/coulomb/spiretemps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class SpireTemperatureSuite extends CoulombSuite:
test("quantity subtraction") {
import coulomb.policy.standard.given

(BigDecimal(100).withTemperature[Celsius] - BigDecimal(90).withUnit[Fahrenheit])
(BigDecimal(100).withTemperature[Celsius] - BigDecimal(90)
.withUnit[Fahrenheit])
.assertDQD[BigDecimal, Celsius](50)
}

Expand Down

0 comments on commit 2369fee

Please sign in to comment.