Skip to content

Commit

Permalink
enable scalafmt (#362)
Browse files Browse the repository at this point in the history
* enable scalafmt

* format build.sbt

* add development section to site doc
  • Loading branch information
erikerlandson authored Oct 29, 2022
1 parent c83e6df commit 4c2f225
Show file tree
Hide file tree
Showing 70 changed files with 3,462 additions and 1,536 deletions.
14 changes: 13 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
version = "3.6.0"
runner.dialect = Scala3
project.includePaths = [] # disables formatting
#project.includePaths = [] # disables formatting
maxColumn = 80
align.preset = some
align.allowOverflow = true
docstrings.style = Asterisk
docstrings.oneline = fold
docstrings.blankFirstLine = yes
docstrings.wrap = no
indent.main = 4
indent.callSite = 4
indent.ctrlSite = 4
indent.defnSite = 4
indent.matchSite = 4
106 changes: 58 additions & 48 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ ThisBuild / tlBaseVersion := "0.6"
ThisBuild / developers += tlGitHubDev("erikerlandson", "Erik Erlandson")
ThisBuild / organization := "com.manyangled"
ThisBuild / organizationName := "Erik Erlandson"
ThisBuild / licenses := List("Apache-2.0" -> url("https://www.apache.org/licenses/LICENSE-2.0.txt"))
ThisBuild / licenses := List(
"Apache-2.0" -> url("https://www.apache.org/licenses/LICENSE-2.0.txt")
)
ThisBuild / startYear := Some(2022)

// ci settings
ThisBuild / tlCiReleaseBranches := Seq("scala3")
// don't overwrite the site published from develop branch for now
// to enable, set this to whatever main branch is to enable (e.g. develop, main, etc)
ThisBuild / tlSitePublishBranch := Some("scala3")

ThisBuild / resolvers ++= Resolver.sonatypeOssRepos("snapshots")
Expand All @@ -29,55 +29,63 @@ def commonSettings = Seq(
)

lazy val root = tlCrossRootProject
.aggregate(core, units, spire, testkit, unidocs)
.aggregate(core, units, spire, testkit, unidocs)

lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("core"))
.settings(name := "coulomb-core")
.settings(commonSettings :_*)
.settings(libraryDependencies += "org.typelevel" %%% "algebra" % "2.8.0")
.platformsSettings(JSPlatform, NativePlatform)(Test / unmanagedSources / excludeFilter := HiddenFileFilter || "*serde.scala")
.crossType(CrossType.Pure)
.in(file("core"))
.settings(name := "coulomb-core")
.settings(commonSettings: _*)
.settings(libraryDependencies += "org.typelevel" %%% "algebra" % "2.8.0")
.platformsSettings(JSPlatform, NativePlatform)(
Test / unmanagedSources / excludeFilter := HiddenFileFilter || "*serde.scala"
)

lazy val units = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("units"))
.settings(name := "coulomb-units")
.dependsOn(core % "compile->compile;test->test")
.settings(commonSettings :_*)
.platformsSettings(JSPlatform, NativePlatform)(libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % "2.4.0" % Test)
.crossType(CrossType.Pure)
.in(file("units"))
.settings(name := "coulomb-units")
.dependsOn(core % "compile->compile;test->test")
.settings(commonSettings: _*)
.platformsSettings(JSPlatform, NativePlatform)(
libraryDependencies += "io.github.cquiroz" %%% "scala-java-time" % "2.4.0" % Test
)

lazy val spire = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("spire"))
.settings(name := "coulomb-spire")
.dependsOn(core % "compile->compile;test->test", units % Test)
.settings(commonSettings :_*)
.settings(libraryDependencies += "org.typelevel" %%% "spire" % "0.18.0")
.crossType(CrossType.Pure)
.in(file("spire"))
.settings(name := "coulomb-spire")
.dependsOn(core % "compile->compile;test->test", units % Test)
.settings(commonSettings: _*)
.settings(libraryDependencies += "org.typelevel" %%% "spire" % "0.18.0")

lazy val testkit = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("testkit"))
.settings(
name := "coulomb-testkit",
libraryDependencies ++= Seq(
"org.scalacheck" %%% "scalacheck" % "1.17.0",
"org.scalameta" %%% "munit-scalacheck" % "1.0.0-M6" % Test,
"org.typelevel" %%% "algebra-laws" % "2.8.0" % Test,
"org.typelevel" %%% "discipline-munit" % "2.0.0-M3" % Test,
.crossType(CrossType.Pure)
.in(file("testkit"))
.settings(
name := "coulomb-testkit",
libraryDependencies ++= Seq(
"org.scalacheck" %%% "scalacheck" % "1.17.0",
"org.scalameta" %%% "munit-scalacheck" % "1.0.0-M6" % Test,
"org.typelevel" %%% "algebra-laws" % "2.8.0" % Test,
"org.typelevel" %%% "discipline-munit" % "2.0.0-M3" % Test
)
)
)
.dependsOn(core % "compile->compile;test->test")
.settings(commonSettings :_*)
.dependsOn(core % "compile->compile;test->test")
.settings(commonSettings: _*)

// a target for rolling up all subproject deps: a convenient
// way to get a repl that has access to all subprojects
// sbt all/console
lazy val all = project
.in(file("all")) // sbt will create this - it is unused
.dependsOn(core.jvm, units.jvm, spire.jvm) // scala repl only needs JVMPlatform subproj builds
.settings(name := "coulomb-all")
.enablePlugins(NoPublishPlugin) // don't publish
.in(file("all")) // sbt will create this - it is unused
.dependsOn(
core.jvm,
units.jvm,
spire.jvm
) // scala repl only needs JVMPlatform subproj builds
.settings(name := "coulomb-all")
.enablePlugins(NoPublishPlugin) // don't publish

// a published artifact aggregating API docs for viewing at javadoc.io
// build and view scaladocs locally:
Expand All @@ -86,23 +94,25 @@ lazy val all = project
// serve locally:
// python3 -m http.server -d unidocs/target/scala-3.1.2/unidoc/
lazy val unidocs = project
.in(file("unidocs")) // sbt will create this
.settings(name := "coulomb-docs") // the name of the artifact
.enablePlugins(TypelevelUnidocPlugin) // enable Unidoc + publishing
.in(file("unidocs")) // sbt will create this
.settings(name := "coulomb-docs") // the name of the artifact
.enablePlugins(TypelevelUnidocPlugin) // enable Unidoc + publishing

// https://typelevel.org/sbt-typelevel/site.html
// sbt docs/tlSitePreview
// http://localhost:4242
lazy val docs = project.in(file("site"))
.dependsOn(core.jvm, units.jvm, spire.jvm)
.enablePlugins(TypelevelSitePlugin)
lazy val docs = project
.in(file("site"))
.dependsOn(core.jvm, units.jvm, spire.jvm)
.enablePlugins(TypelevelSitePlugin)

// https://github.com/sbt/sbt-jmh
// sbt "benchmarks/Jmh/run .*Benchmark"
lazy val benchmarks = project.in(file("benchmarks"))
.dependsOn(core.jvm % "compile->compile;compile->test")
.settings(name := "coulomb-benchmarks")
.enablePlugins(NoPublishPlugin, JmhPlugin)
lazy val benchmarks = project
.in(file("benchmarks"))
.dependsOn(core.jvm % "compile->compile;compile->test")
.settings(name := "coulomb-benchmarks")
.enablePlugins(NoPublishPlugin, JmhPlugin)

// can enable this to add benchmarks to CI
// ThisBuild / githubWorkflowBuild += WorkflowStep.Sbt(List("benchmarks/Jmh/run .*Benchmark"))
77 changes: 61 additions & 16 deletions core/src/main/scala/coulomb/conversion/coefficients.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,103 @@ package coulomb.conversion
import coulomb.rational.Rational

object coefficients:
inline def coefficientRational[U1, U2]: Rational = ${ meta.coefficientRational[U1, U2] }
inline def coefficientDouble[U1, U2]: Double = ${ meta.coefficientDouble[U1, U2] }
inline def coefficientFloat[U1, U2]: Float = ${ meta.coefficientFloat[U1, U2] }
inline def coefficientRational[U1, U2]: Rational = ${
meta.coefficientRational[U1, U2]
}
inline def coefficientDouble[U1, U2]: Double = ${
meta.coefficientDouble[U1, U2]
}
inline def coefficientFloat[U1, U2]: Float = ${
meta.coefficientFloat[U1, U2]
}

inline def coefficientNumDouble[U1, U2]: Double = ${ meta.coefficientNumDouble[U1, U2] }
inline def coefficientDenDouble[U1, U2]: Double = ${ meta.coefficientDenDouble[U1, U2] }
inline def coefficientNumDouble[U1, U2]: Double = ${
meta.coefficientNumDouble[U1, U2]
}
inline def coefficientDenDouble[U1, U2]: Double = ${
meta.coefficientDenDouble[U1, U2]
}

inline def deltaOffsetRational[U, B]: Rational = ${ meta.deltaOffsetRational[U, B] }
inline def deltaOffsetDouble[U, B]: Double = ${ meta.deltaOffsetDouble[U, B] }
inline def deltaOffsetRational[U, B]: Rational = ${
meta.deltaOffsetRational[U, B]
}
inline def deltaOffsetDouble[U, B]: Double = ${
meta.deltaOffsetDouble[U, B]
}
inline def deltaOffsetFloat[U, B]: Float = ${ meta.deltaOffsetFloat[U, B] }

object meta:
import scala.quoted.*
import coulomb.infra.meta.{*, given}

def coefficientRational[U1, U2](using Quotes, Type[U1], Type[U2]): Expr[Rational] =
def coefficientRational[U1, U2](using
Quotes,
Type[U1],
Type[U2]
): Expr[Rational] =
import quotes.reflect.*
val c = coef(TypeRepr.of[U1], TypeRepr.of[U2])
Expr(c)

def coefficientDouble[U1, U2](using Quotes, Type[U1], Type[U2]): Expr[Double] =
def coefficientDouble[U1, U2](using
Quotes,
Type[U1],
Type[U2]
): Expr[Double] =
import quotes.reflect.*
val c = coef(TypeRepr.of[U1], TypeRepr.of[U2])
Expr(c.toDouble)

def coefficientFloat[U1, U2](using Quotes, Type[U1], Type[U2]): Expr[Float] =
def coefficientFloat[U1, U2](using
Quotes,
Type[U1],
Type[U2]
): Expr[Float] =
import quotes.reflect.*
val c = coef(TypeRepr.of[U1], TypeRepr.of[U2])
Expr(c.toFloat)

def coefficientNumDouble[U1, U2](using Quotes, Type[U1], Type[U2]): Expr[Double] =
def coefficientNumDouble[U1, U2](using
Quotes,
Type[U1],
Type[U2]
): Expr[Double] =
import quotes.reflect.*
val c = coef(TypeRepr.of[U1], TypeRepr.of[U2])
Expr(c.n.toDouble)

def coefficientDenDouble[U1, U2](using Quotes, Type[U1], Type[U2]): Expr[Double] =
def coefficientDenDouble[U1, U2](using
Quotes,
Type[U1],
Type[U2]
): Expr[Double] =
import quotes.reflect.*
val c = coef(TypeRepr.of[U1], TypeRepr.of[U2])
Expr(c.d.toDouble)

def deltaOffsetRational[U, B](using Quotes, Type[U], Type[B]): Expr[Rational] =
def deltaOffsetRational[U, B](using
Quotes,
Type[U],
Type[B]
): Expr[Rational] =
import quotes.reflect.*
val doff = offset(TypeRepr.of[U], TypeRepr.of[B])
Expr(doff)

def deltaOffsetDouble[U, B](using Quotes, Type[U], Type[B]): Expr[Double] =
def deltaOffsetDouble[U, B](using
Quotes,
Type[U],
Type[B]
): Expr[Double] =
import quotes.reflect.*
val doff = offset(TypeRepr.of[U], TypeRepr.of[B])
Expr(doff.toDouble)

def deltaOffsetFloat[U, B](using Quotes, Type[U], Type[B]): Expr[Float] =
def deltaOffsetFloat[U, B](using
Quotes,
Type[U],
Type[B]
): Expr[Float] =
import quotes.reflect.*
val doff = offset(TypeRepr.of[U], TypeRepr.of[B])
Expr(doff.toFloat)

20 changes: 15 additions & 5 deletions core/src/main/scala/coulomb/conversion/conversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,30 @@ import scala.annotation.implicitNotFound
@implicitNotFound("No value conversion in scope for value types ${VF} => ${VT}")
abstract class ValueConversion[VF, VT] extends (VF => VT)

@implicitNotFound("No truncating value conversion in scope for value types ${VF} => ${VT}")
@implicitNotFound(
"No truncating value conversion in scope for value types ${VF} => ${VT}"
)
abstract class TruncatingValueConversion[VF, VT] extends (VF => VT)

/** Convert a value of type V from implied units UF to UT */
@implicitNotFound("No unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}")
@implicitNotFound(
"No unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}"
)
abstract class UnitConversion[V, UF, UT] extends (V => V)

@implicitNotFound("No truncating unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}")
@implicitNotFound(
"No truncating unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}"
)
abstract class TruncatingUnitConversion[V, UF, UT] extends (V => V)

/** Convert a value of type V from implied delta units UF to UT */
@implicitNotFound("No unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}")
@implicitNotFound(
"No unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}"
)
abstract class DeltaUnitConversion[V, B, UF, UT] extends (V => V)

/** Convert a value of type V from implied delta units UF to UT */
@implicitNotFound("No truncating unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}")
@implicitNotFound(
"No truncating unit conversion in scope for value type ${V}, unit types ${UF} => ${UT}"
)
abstract class TruncatingDeltaUnitConversion[V, B, UF, UT] extends (V => V)
15 changes: 8 additions & 7 deletions core/src/main/scala/coulomb/conversion/standard/scala.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,34 @@ object scala:
import coulomb.*
import coulomb.syntax.*

// Enable the compiler to implicitly convert Quantity[V1, U1] -> Quantity[V2, U2],
// Enable the compiler to implicitly convert Quantity[V1, U1] -> Quantity[V2, U2],
// whenever a valid conversion exists:
// https://docs.scala-lang.org/scala3/reference/contextual/conversions.html

given ctx_Quantity_Conversion_1V1U[V, U]: Conversion[Quantity[V, U], Quantity[V, U]] =
given ctx_Quantity_Conversion_1V1U[V, U]
: Conversion[Quantity[V, U], Quantity[V, U]] =
(q: Quantity[V, U]) => q

given ctx_Quantity_Conversion_1V2U[V, UF, UT](using
uc: UnitConversion[V, UF, UT]
): Conversion[Quantity[V, UF], Quantity[V, UT]] =
): Conversion[Quantity[V, UF], Quantity[V, UT]] =
(q: Quantity[V, UF]) => uc(q.value).withUnit[UT]

given ctx_Quantity_Conversion_2V1U[U, VF, VT](using
vc: ValueConversion[VF, VT],
): Conversion[Quantity[VF, U], Quantity[VT, U]] =
vc: ValueConversion[VF, VT]
): Conversion[Quantity[VF, U], Quantity[VT, U]] =
(q: Quantity[VF, U]) => vc(q.value).withUnit[U]

given ctx_Quantity_Conversion_2V2U[VF, UF, VT, UT](using
vc: ValueConversion[VF, VT],
uc: UnitConversion[VT, UF, UT]
): Conversion[Quantity[VF, UF], Quantity[VT, UT]] =
): Conversion[Quantity[VF, UF], Quantity[VT, UT]] =
(q: Quantity[VF, UF]) => uc(vc(q.value)).withUnit[UT]

given ctx_DeltaQuantity_conversion_2V2U[B, VF, UF, VT, UT](using
vc: ValueConversion[VF, VT],
uc: DeltaUnitConversion[VT, B, UF, UT]
): Conversion[DeltaQuantity[VF, UF, B], DeltaQuantity[VT, UT, B]] =
): Conversion[DeltaQuantity[VF, UF, B], DeltaQuantity[VT, UT, B]] =
(q: DeltaQuantity[VF, UF, B]) => uc(vc(q.value)).withDeltaUnit[UT, B]

// also support implicit lift of values to unitless quantity
Expand Down
Loading

0 comments on commit 4c2f225

Please sign in to comment.