monix.newtypes

package monix.newtypes

Members list

Packages

Type members

Classlikes

final case class BuildFailure[T](typeInfo: TypeInfo[T], message: Option[String])

Signals a failed decoding operation, via HasBuilder.build.

Signals a failed decoding operation, via HasBuilder.build.

For example, this can happen when decoding from JSON, this result being used to signal an issue with the JSON document.

Attributes

Companion
object
Source
HasBuilder.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object BuildFailure

Attributes

Companion
class
Source
HasBuilder.scala
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
trait HasBuilder[Type]

Type-class used for decoding types.

Type-class used for decoding types.

Used for automatically deriving decoders (e.g. from JSON) for newtypes.

Attributes

See also

HasExtractor for deriving encoders.

Companion
object
Source
HasBuilder.scala
Supertypes
class Object
trait Matchable
class Any
object HasBuilder

Attributes

Companion
trait
Source
HasBuilder.scala
Supertypes
class Object
trait Matchable
class Any
Self type
HasBuilder.type
trait HasExtractor[Type]

Type-class used for encoding types.

Type-class used for encoding types.

Used for automatically deriving encoders (e.g. to JSON) for newtypes.

Attributes

See also

HasBuilder for deriving decoders.

Companion
object
Source
HasExtractor.scala
Supertypes
class Object
trait Matchable
class Any
object HasExtractor

Attributes

Companion
trait
Source
HasExtractor.scala
Supertypes
class Object
trait Matchable
class Any
Self type
abstract class Newsubtype[Src]

Like Newtype, except as a subtype of the underlying type instead of as an entirely new type.

Like Newtype, except as a subtype of the underlying type instead of as an entirely new type.

 type Level = Level.Type
 object Level extends NewsubtypeWrapped[Int]

 val level1: Level = Level(1)
 // This works too
 val level2: Int = Level(2)
 // This fails compilation
 // val level3: Level = 3

Attributes

Source
Newsubtype.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class NewsubtypeValidated[Src]
class NewsubtypeWrapped[Src]
abstract class NewsubtypeCovariantK[Src[_]]

Similar to NewtypeCovariantK, except that the type created is a sub-type.

Similar to NewtypeCovariantK, except that the type created is a sub-type.

Attributes

Source
NewsubtypeK.scala
Supertypes
class Object
trait Matchable
class Any
abstract class NewsubtypeK[Src[_]]

Similar to NewtypeK, except that the type created is a sub-type.

Similar to NewtypeK, except that the type created is a sub-type.

Attributes

Source
NewsubtypeK.scala
Supertypes
class Object
trait Matchable
class Any
abstract class NewsubtypeValidated[Src] extends Newsubtype[Src]

A validated Newsubtype.

A validated Newsubtype.

This class is for defining newsubtypes with a builder that validates values.

Example:

 type EmailAddress = EmailAddress.Type

 object EmailAddress extends NewsubtypeValidated[String] {
   def apply(v: String): Either[BuildFailure[Type], Type] =
     if (v.contains("@"))
       Right(unsafeCoerce(v))
     else
       Left(BuildFailure[EmailAddress]("missing @"))
 }

Attributes

Source
NewValidated.scala
Supertypes
class Newsubtype[Src]
class Object
trait Matchable
class Any
abstract class NewsubtypeWrapped[Src] extends Newsubtype[Src]

Simple variant of Newsubtype that provides an apply builder.

Simple variant of Newsubtype that provides an apply builder.

Such newsubtypes are meant for simple wrappers that don't do any validation.

Usage:

 type FullName = FullName.Type

 object FullName extends NewsubtypeWrapped[String]

 // Initializing
 val name: FullName = FullName("Alexandru Nedelcu")
 // No need to extract the value when a string is needed:
 assert(name === "Alexandru Nedelcu")

 // We can pattern-match too:
 name match {
   case FullName(nameStr) =>
     assert(nameStr === "Alexandru Nedelcu")
 }

Attributes

Source
NewWrapped.scala
Supertypes
class Newsubtype[Src]
class Object
trait Matchable
class Any
abstract class Newtype[Src]

Base class for defining newtypes that have no type parameters.

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 apply builder.

Newsubtype for defining subtypes of the underlying type.

Source
Newtype.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class NewtypeValidated[Src]
class NewtypeWrapped[Src]
abstract class NewtypeCovariantK[Src[_]]

For building newtypes over types that have a covariant type parameter (higher-kinded types).

For building newtypes over types that have a covariant type parameter (higher-kinded types).

Example:

 // Only needed for type-class derivation
 import cats._
 import cats.implicits._

 type NonEmptyList[A] = NonEmptyList.Type[A]

 object NonEmptyList extends NewtypeCovariantK[List] {
   def apply[A](head: A, tail: A*): NonEmptyList[A] =
     unsafeCoerce(head :: tail.toList)

   def unapply[F[_], A](list: F[A])(
     implicit ev: F[A] =:= NonEmptyList[A]
   ): Some[(A, List[A])] = {
     val l = value(list)
     Some((l.head, l.tail))
   }

   implicit def eq[A: Eq]: Eq[NonEmptyList[A]] =
     derive
   implicit val traverse: Traverse[NonEmptyList] =
     deriveK
   implicit val monad: Monad[NonEmptyList] =
     deriveK
 }

NOTE: the type-parameter is covariant.

Attributes

See also

NewtypeK for working with invariance.

Source
NewtypeK.scala
Supertypes
class Object
trait Matchable
class Any
abstract class NewtypeK[Src[_]]

For building newtypes over types that have an invariant type parameter (higher-kinded types).

For building newtypes over types that have an invariant type parameter (higher-kinded types).

Example:

 // Only needed for type-class derivation
 import cats._
 import cats.implicits._

 type Nel[A] = Nel.Type[A]

 object Nel extends NewtypeK[List] {
   def apply[A](head: A, tail: A*): Nel[A] =
     unsafeCoerce(head :: tail.toList)

   def unapply[F[_], A](list: F[A])(
     implicit ev: F[A] =:= Nel[A]
   ): Some[(A, List[A])] = {
     val l = value(list)
     Some((l.head, l.tail))
   }

   implicit def eq[A: Eq]: Eq[Nel[A]] =
     derive
   implicit val traverse: Traverse[Nel] =
     deriveK
   implicit val monad: Monad[Nel] =
     deriveK
 }

NOTE: the type-parameter is invariant. See NewtypeCovariantK for working with covariant type parameters.

Attributes

Source
NewtypeK.scala
Supertypes
class Object
trait Matchable
class Any
abstract class NewtypeValidated[Src] extends Newtype[Src]

A validated Newtype.

A validated Newtype.

This class is for defining newtypes with a builder that validates values.

Example:

 type EmailAddress = EmailAddress.Type

 object EmailAddress extends NewtypeValidated[String] {
   def apply(v: String): Either[BuildFailure[Type], Type] =
     if (v.contains("@"))
       Right(unsafeCoerce(v))
     else
       Left(BuildFailure("missing @"))
 }

Attributes

Source
NewValidated.scala
Supertypes
class Newtype[Src]
class Object
trait Matchable
class Any
abstract class NewtypeWrapped[Src] extends Newtype[Src]

Simple variant of Newtype that provides an apply builder.

Simple variant of Newtype that provides an apply builder.

Such newtypes are meant for simple wrappers that don't do any validation.

Usage:

 type FullName = FullName.Type

 object FullName extends NewtypeWrapped[String]

 // Initializing
 val name: FullName = FullName("Alexandru Nedelcu")
 // Extracting the value when a string is needed:
 val nameStr: String = name.value
 assert(nameStr === "Alexandru Nedelcu")

 // We can pattern-match too:
 name match {
   case FullName(nameStr) =>
     assert(nameStr === "Alexandru Nedelcu")
 }

Attributes

Source
NewWrapped.scala
Supertypes
class Newtype[Src]
class Object
trait Matchable
class Any
final case class TypeInfo[T](typeName: String, typeLabel: String, packageName: String, typeParams: List[Option[TypeInfo[_]]])

TypeInfo values are used as replacement for ClassTag, to extract type info at compile-time (name, package) and use that for debugging purposes (e.g. better error messages).

TypeInfo values are used as replacement for ClassTag, to extract type info at compile-time (name, package) and use that for debugging purposes (e.g. better error messages).

Attributes

Companion
object
Source
TypeInfo.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object TypeInfo

Attributes

Companion
class
Source
TypeInfo.scala
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
TypeInfo.type