Newtype
Base class for defining newtypes that have no type parameters.
This class does not define any "builder", or related HasBuilder instance, as you're expected to provide one yourself.
Usage sample:
type EmailAddress = EmailAddress.Type
object EmailAddress extends Newtype[String] { self =>
def apply(value: String): Option[Type] =
if (value.contains("@"))
Some(unsafeCoerce(value))
else
None
// Recommended instance, but not required;
// use Newtype.Validated to get rid of this boilerplate ;-)
implicit val builder: HasBuilder.Aux[EmailAddress, String] =
new HasBuilder[EmailAddress] {
type Source = String
def build(v: String): Either[BuildFailure[Type], Type] =
apply(v) match {
case Some(r) =>
Right(r)
case None =>
Left(BuildFailure[EmailAddress]("missing @"))
}
}
}
- See also:
NewtypeWrapped and NewtypeValidated for variants that provide an
apply
builder.Newsubtype for defining subtypes of the underlying type.
trait NewtypeTrait[Src]
trait NewEncoding[Src]
class Object
trait Matchable
class Any
class NewtypeValidated[Src]
class NewtypeWrapped[Src]
Type members
Inherited types
Value members
Inherited methods
Extensions
Inherited extensions
extension (self: Type)