• Stars
    star
    105
  • Rank 316,554 (Top 7 %)
  • Language
    Scala
  • Created about 1 year ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A friendly newtype library for Scala 3

neotype

Release Artifacts Snapshot Artifacts

A friendly newtype library for Scala 3.

"io.github.kitlangton" %% "neotype" % "0.2.4"

Features

  • Compile-time Checked Values
  • Write validations as plain, old Scala expressions
  • Helpful compilation errors (see below)
  • No runtime allocations (Thanks to inline and opaque type)
  • Integrates with other libraries (e.g. zio-json, circe, tapir, etc.)
NEOTYPE VIDEO TOUR

Example

Here is how to define a compile-time validated Newtype.

import neotype.*

// 1. Define a newtype.
object NonEmptyString extends Newtype[String]:

  // 2. Optionally, define a validate method.
  override inline def validate(input: String): Boolean =
    input.nonEmpty

// 3. Construct values.
NonEmptyString("Hello") // OK
NonEmptyString("")      // Compile Error

Attempting to call NonEmptyString("") would result in the following compilation error:

Error: /src/main/scala/examples/Main.scala:9:16
  NonEmptyString("")
  ^^^^^^^^^^^^^^^^^^
  —— Newtype Error ——————————————————————————————————————————————————————————
  NonEmptyString was called with an INVALID String.
  input: ""
  check: input.nonEmpty
  ———————————————————————————————————————————————————————————————————————————

Integrations

Neotype integrates with the following libraries.

  • zio-test DeriveGen
  • zio-json
  • zio-config
  • zio-schema
  • play-json
  • tapir
  • quill
  • circe
  • jsoniter

ZIO Json Example

import neotype.*
import neotype.ziojson.given
import zio.json.*

type NonEmptyString = NonEmptyString.Type
object NonEmptyString extends Newtype[String]:
  override inline def validate(value: String): Result =
    if value.nonEmpty then true else "String must not be empty"

case class Person(name: NonEmptyString, age: Int) derives JsonCodec

val parsed = """{"name": "Kit", "age": 30}""".fromJson[Person]
// Right(Person(NonEmptyString("Kit"), 30))

val failed = """{"name": "", "age": 30}""".fromJson[Person]
// Left(".name(String must not be empty)")

By importing neotype.ziojson.given, we automatically generate a JsonCodec for NonEmptyString. Custom failure messages are also supported (by overriding def failureMessage in the Newtype definition).

More Repositories

1

zio-magic

Construct ZLayers automagically (w/ helpful compile-time errors)
Scala
253
star
2

scala-update

Update your Scala dependencies interactively
Scala
130
star
3

parallel-for

Automatically parallelize your for-comprehensions at compile time.
Scala
130
star
4

zio-app

Quickly create and develop full-stack Scala apps with ZIO and Laminar.
Scala
122
star
5

OmenTextField

A better TextField for SwiftUI. A growing, multiline, auto-focusable TextField supporting bindable focus.
Swift
115
star
6

animus

An FRP animation library for Laminar
Scala
90
star
7

quotidian

A menagerie of macro utilities and extensions for Scala 3
Scala
86
star
8

zio-catechism

Animated ZIO documentation and visualizations
Scala
57
star
9

zio-tui

Scala
54
star
10

zio-slides

An interactive, websocket-backed slide presentation app.
Scala
48
star
11

formula

Form Combinator Library for decimating frontend boilerplate.
Scala
33
star
12

conclave

An elegant and perfect meetup application
Scala
25
star
13

zio-api

An API DSL for constructing servers, clients, and documentation.
Scala
24
star
14

zio-start

A wizard for generating new ZIO applications.
Scala
20
star
15

migraine

A minimalist DB migration manager.
Scala
18
star
16

idiomatic-zio-app-architecture

This repo contains the code for a series of Zymposium videos on ZIO App Architecture and ZLayers
Scala
13
star
17

zio-from-scatch

Rebuilding ZIO from scratch for fun and (educational) profit
Scala
12
star
18

automaton

RPC + GPT
Scala
9
star
19

Spandex

A Snippet App for macOS
Swift
7
star
20

java-to-scala

This repository contains the exercises and examples for the "Java to Scala" course.
Scala
6
star
21

eulerplate

A tool for generating Haskell boilerplate for solving Hacker Rank challenges.
Haskell
5
star
22

scala-school-site

5
star
23

scala-3-compile-time-business

scala-3-compile-time-business
Scala
5
star
24

boilerplate

A DSL for boilerplate generation (A counter-hex against the evil number 22)
Scala
4
star
25

compile-time-time

A repository for the Compile-Time Time videos
Scala
4
star
26

kitcoin

An animated bitcoin-esque (blockchain/consensus) network simulator
PureScript
3
star
27

scala-macro-fun

FuN ScAlA MaCrOs!
Scala
3
star
28

Swift-Observation-From-Scratch

Reimplementing Swift's Observation functionality
Swift
3
star
29

raft-skeleton

raft
Scala
3
star
30

type-inference

type-inference
Scala
2
star
31

OSSRH-62496

What a repo!
2
star
32

zio-trials

Interactive ZIO exercises
Scala
2
star
33

zio-hackathon

Scala
2
star
34

quill-examples-zymposium

Scala
2
star
35

zio-play-example

Scala
2
star
36

hack-hackery

hacking on HACK ASM
Assembly
2
star
37

local-deps-example

A sane local dependency workflow for Scala and sbt
Scala
2
star
38

declarative-vs-executable

Scala
2
star
39

snaked

A multiplayer, socket-based, command-line snake game.
Haskell
2
star
40

jmh-benchmark-action

A GitHub CI Action for automating the comparison of JMH benchmarking results.
Scala
2
star
41

Swipe-Actions-from-Scratch

Code for the Swipe Actions from Scratch video series
Swift
2
star
42

Spring-Animation-From-Scratch

Code for the Spring Animation From Scratch Videos
Swift
2
star
43

OmenGridAnimation

Swift
1
star
44

job_hunger_ember

JavaScript
1
star
45

okasaki

Scala Explorations based upon Chris Okasaki's Purely Functional Data Structures
Scala
1
star
46

au_pair

Ruby
1
star
47

scala-symposia

Executable Scala lessons
Scala
1
star
48

Rosalind-Problems

Ruby
1
star
49

attr_asker

Ruby
1
star
50

AtomSetup

JavaScript
1
star