constructs a new CaseClass instance
constructs a new CaseClass instance
This method is intended to be called only from code generated by the Magnolia macro, and should not be called directly from users' code.
derives a generic typeclass instance for the type T
derives a generic typeclass instance for the type T
This is a macro definition method which should be bound to a method defined inside a Magnolia
generic derivation object, that is, one which defines the methods combine
, dispatch
and
the type constructor, Typeclass[_]
. This will typically look like,
object Derivation { // other definitions implicit def gen[T]: Typeclass[T] = Magnolia.gen[T] }which would support automatic derivation of typeclass instances by calling
Derivation.gen[T]
or with implicitly[Typeclass[T]]
, if the implicit method is imported
into the current scope. The definition expects a type constructor called Typeclass
, taking one *-kinded type
parameter to be defined on the same object as a means of determining how the typeclass should
be genericized. While this may be obvious for typeclasses like Show[T]
which take only a
single type parameter, Magnolia can also derive typeclass instances for types such as
Decoder[Format, Type]
which would typically fix the Format
parameter while varying the
Type
parameter.
While there is no "interface" for a derivation, in the object-oriented sense, the Magnolia macro expects to be able to call certain methods on the object within which it is bound to a method.
Specifically, for deriving case classes (product types), the macro will attempt to call the
combine
method with an instance of CaseClass, like so,
<derivation>.combine(<caseClass>): Typeclass[T]That is to say, the macro expects there to exist a method called
combine
on the derivation
object, which may be called with the code above, and for it to return a type which conforms
to the type Typeclass[T]
. The implementation of combine
will therefore typically look
like this,
def combine[T](caseClass: CaseClass[Typeclass, T]): Typeclass[T] = ...however, there is the flexibility to provide additional type parameters or additional implicit parameters to the definition, provided these do not affect its ability to be invoked as described above.
Likewise, for deriving sealed traits (coproduct or sum types), the macro will attempt to call
the dispatch
method with an instance of SealedTrait, like so,
<derivation>.dispatch(<sealedTrait>): Typeclass[T]so a definition such as,
def dispatch[T](sealedTrait: SealedTrait[Typeclass, T]): Typeclass[T] = ...will suffice, however the qualifications regarding additional type parameters and implicit parameters apply equally to
dispatch
as to combine
.
constructs a new Param instance
constructs a new Param instance
This method is intended to be called only from code generated by the Magnolia macro, and should not be called directly from users' code.
constructs a new Subtype instance
constructs a new Subtype instance
This method is intended to be called only from code generated by the Magnolia macro, and should not be called directly from users' code.
the object which defines the Magnolia macro