A friendly newtype library for Scala 3.
"io.github.kitlangton" %% "neotype" % "0.0.5"
- Compile-time Checked Values
- Write validations as plain, old Scala expressions
- Very Helpful Error Messages (see below)
- No Runtime Allocations (Thanks to
inline
andopaque type
) - Ability to integrate with other libraries (e.g.
zio-json
,circe
, etc.)
import neotype.*
// Define a newtype:
given NonEmptyString: Newtype[String] with
// Hey, a plain old Scala expression!
inline def validate(input: String): Boolean =
input.nonEmpty
// Wrap values, which checked at compile-time:
NonEmptyString("Hello") // OK
NonEmptyString("") // Compile Error
Error: /Users/kit/code/neotype/examples/src/main/scala/neotype/examples/Main.scala:9:16
NonEmptyString("")
^^^^^^^^^^^^^^^^^^
—— Newtype Error ——————————————————————————————————————————————————————————
NonEmptyString was called with an INVALID String.
input: ""
check: input.nonEmpty
———————————————————————————————————————————————————————————————————————————