Skip to content

yawaramin/newtype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Newtype

Named after the Haskell 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:

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:

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

About

A newtype (non-allocating) wrapper type

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages