Newtype

abstract class Newtype[Src] extends NewtypeTrait[Src]

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

override opaque type Type
Inherited from:
NewtypeTrait

Value members

Inherited methods

final inline protected def derive[F[_]](implicit ev: F[Src]): F[Type]
Inherited from:
NewEncoding
final inline protected def unsafeCoerce(value: Src): Type
Inherited from:
NewEncoding

Extensions

Inherited extensions

extension (self: Type)
final inline def value: Src
Inherited from:
NewEncoding

Implicits

Inherited implicits

implicit val codec: Aux[Type, Src]
Inherited from:
NewEncoding
implicit val typeInfo: TypeInfo[Type]
Inherited from:
NewEncoding