Skip to content

A friendly newtype library for Scala 3

Notifications You must be signed in to change notification settings

soujiro32167/neotype

 
 

Repository files navigation

neotype

Release Artifacts Snapshot Artifacts

A friendly newtype library for Scala 3.

"io.github.kitlangton" %% "neotype" % "0.0.5"

Features

  • Compile-time Checked Values
  • Write validations as plain, old Scala expressions
  • Very Helpful Error Messages (see below)
  • No Runtime Allocations (Thanks to inline and opaque type)
  • Ability to integrate with other libraries (e.g. zio-json, circe, etc.)

Example

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
  ———————————————————————————————————————————————————————————————————————————

About

A friendly newtype library for Scala 3

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 100.0%