https://github.com/yawaramin/newtype
A newtype (non-allocating) wrapper type
https://github.com/yawaramin/newtype
newtype scala
Last synced: 9 months ago
JSON representation
A newtype (non-allocating) wrapper type
- Host: GitHub
- URL: https://github.com/yawaramin/newtype
- Owner: yawaramin
- License: mit
- Created: 2018-12-08T06:40:46.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-07T15:13:08.000Z (over 7 years ago)
- Last Synced: 2025-10-04T08:58:57.005Z (9 months ago)
- Topics: newtype, scala
- Language: Scala
- Size: 63.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Newtype
Named after the [Haskell Newtype](https://wiki.haskell.org/Newtype), a
Scala module to help creating non-allocating wrapper types for existing
types, if you're looking for an alternative to `AnyVal` and the
'tagging' technique introduced by Scalaz and others.
## Use
Define a new wrapper:
```scala
import newtype.Newtype
object Types {
val Id: Newtype[Long] = Newtype(0.<)
type Id = Id.Type
}
```
The `Newtype.apply` smart constructor provides a module containing the
newly-minted type and a set of operations to create and extract values
of the type:
```scala
object TypesTest {
import Types._
/* The constraint will prevent creating invalid values by throwing a
runtime exception: */
//val invalidId = Id(0)
val bobId: Id = Id(1)
val 1L = Id.value(bobId)
}
```
## Develop
* Add `-t` to enable watch mode
* Add `--build-cache` to reuse outputs from previous builds (recommended
with `-t`)
Compile:
./gradlew compileScala
Test:
./gradlew test