Packages

p

monix

newtypes

package newtypes

Ordering
  1. Alphabetic
Visibility
  1. Public
  2. Protected

Type Members

  1. final case class BuildFailure[T](typeInfo: TypeInfo[T], message: Option[String]) extends Product with Serializable

    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.

  2. trait HasBuilder[Type] extends AnyRef

    Type-class used for decoding types.

    Type-class used for decoding types.

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

    See also

    HasExtractor for deriving encoders.

  3. trait HasExtractor[Type] extends AnyRef

    Type-class used for encoding types.

    Type-class used for encoding types.

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

    See also

    HasBuilder for deriving decoders.

  4. abstract class Newsubtype[Src] extends NewsubtypeTrait[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
  5. abstract class NewsubtypeCovariantK[Src[+_]] extends NewsubtypeCovariantTraitK[Src]

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

  6. abstract class NewsubtypeK[Src[_]] extends NewsubtypeTraitK[Src]

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

  7. abstract class NewsubtypeValidated[Src] extends Newsubtype[Src] with NewValidated[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 @"))
    }
  8. abstract class NewsubtypeWrapped[Src] extends Newsubtype[Src] with NewWrapped[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")
    }
  9. abstract class Newtype[Src] extends NewtypeTrait[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 @"))
            }
        }
    }
    See also

    NewtypeWrapped and NewtypeValidated for variants that provide an apply builder.

    Newsubtype for defining _subtypes_ of the underlying type.

  10. abstract class NewtypeCovariantK[Src[+_]] extends NewtypeCovariantTraitK[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.

    See also

    NewtypeK for working with invariance.

  11. abstract class NewtypeK[Src[_]] extends NewtypeTraitK[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.

  12. abstract class NewtypeValidated[Src] extends Newtype[Src] with NewValidated[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 @"))
    }
  13. abstract class NewtypeWrapped[Src] extends Newtype[Src] with NewWrapped[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")
    }
  14. final case class TypeInfo[T](typeName: String, typeLabel: String, packageName: String, typeParams: List[Option[TypeInfo[_]]]) extends Product with Serializable

    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.

    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).

Value Members

  1. object BuildFailure extends Serializable
  2. object HasBuilder
  3. object HasExtractor
  4. object TypeInfo extends TypeInfoLevel0 with Serializable

Ungrouped