abstract class Subtype[A] extends Newtype[A]
- Alphabetic
- By Inheritance
- Subtype
- Newtype
- NewtypeVersionSpecific
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new Subtype()
Type Members
- abstract type Type <: A
- Definition Classes
- Subtype → NewtypeVersionSpecific
- type Wrapped = A
- Definition Classes
- Newtype
Value Members
- macro def apply(value: A, values: A*): NonEmptyChunk[Type]
Converts multiple instances of the underlying type to NonEmptyChunk of instances of the newtype.
Converts multiple instances of the underlying type to NonEmptyChunk of instances of the newtype.
If there is a
def assertion
(see assert), each value will be checked at compile-time.- Definition Classes
- Newtype
- macro def apply(value: A): Type
Converts an instance of the underlying type to an instance of the newtype.
- macro def assert(assertion: Assertion[A]): QuotedAssertion[A]
This method is used to generate Newtype that can be validated at compile-time.
This method is used to generate Newtype that can be validated at compile-time. This must wrap a Assertion and be assigned to
def assertion
.For example, here is a refined Newtype for Natural numbers. Natural numbers are whole numbers greater than or equal to 0.
import zio.prelude.Subtype import zio.prelude.Assertion._ type Natural = Natural.Type object Natural extends Subtype[Int] { def assertion = assert(greaterThanOrEqualTo(0)) }
With this
assertion
defined,Natural.apply
will check literal values at compile-time, failing with an error message if the Assertion is not satisfied.Natural(-10)
would render "-10 failed to satisfy greaterThanOrEqualTo(10)
"IMPORTANT: Due to the macro machinery powering this feature, you must be sure to NOT ANNOTATE
def assertion
with a type (QuotedAssertion
). If you do so, the macro will not be able to run the provided assertion at compile-time and will fail with a message containing this very same information.- Definition Classes
- Newtype
- macro def assertCustom(f: (A) => Either[AssertionError, Unit]): QuotedAssertion[A]
This method is used as an escape hatch for
assert
to allow Newtypes to use custom functions.This method is used as an escape hatch for
assert
to allow Newtypes to use custom functions. If at all possible, you should use assert instead.This will use whitebox.Context.eval under the hood, so all limitations of that function apply here. See here for details: https://github.com/scala/scala/blob/2.13.x/src/reflect/scala/reflect/macros/Evals.scala#L69
IMPORTANT: Due to the macro machinery powering this feature, you must be sure to NOT ANNOTATE
def assertion
with a type (QuotedAssertion
). If you do so, the macro will not be able to run the provided assertion at compile-time and will fail with a message containing this very same information.- Definition Classes
- Newtype
- def assertion: QuotedAssertion[A]
- Definition Classes
- Newtype
- macro def make(value: A): Validation[String, Type]
- Definition Classes
- Newtype
- macro def makeAll[F[+_]](value: F[A])(implicit arg0: ForEach[F]): Validation[String, F[Type]]
- Definition Classes
- Newtype
- def unapply(value: Type): Some[A]
Allows pattern matching on newtype instances to convert them back to instances of the underlying type.
Allows pattern matching on newtype instances to convert them back to instances of the underlying type.
- Definition Classes
- Newtype
- def unwrap(value: Type): A
Converts an instance of the newtype back to an instance of the underlying type.
Converts an instance of the newtype back to an instance of the underlying type.
- Definition Classes
- Newtype
- def unwrapAll[F[_]](value: F[Type]): F[A]
Converts an instance of a type parameterized on the newtype back to an instance of a type parameterized on the underlying type.
Converts an instance of a type parameterized on the newtype back to an instance of a type parameterized on the underlying type. For example, this could be used to convert a list of instances of the newtype back to a list of instances of the underlying type.
- Definition Classes
- Newtype
- macro def wrapAll[F[_]](value: F[A]): F[Type]
Converts an instance of a type parameterized on the underlying type to an instance of a type parameterized on the newtype.
Converts an instance of a type parameterized on the underlying type to an instance of a type parameterized on the newtype. For example, this could be used to convert a list of instances of the underlying type to a list of instances of the newtype.
Due to macro limitations, this method cannot with refined newtype and will thus issue a compiler error if you attempt to do so.
- Definition Classes
- Newtype