Newtype
monix.newtypes.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 @"))
}
}
}
Attributes
- See also
-
NewtypeWrapped and NewtypeValidated for variants that provide an
applybuilder.Newsubtype for defining subtypes of the underlying type.
- Source
- Newtype.scala
- Graph
-
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class NewtypeValidated[Src]class NewtypeWrapped[Src]
Members list
In this article