object Magnolia
the object which defines the Magnolia macro
- Alphabetic
- By Inheritance
- Magnolia
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##(): Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def gen[T](c: Context)(implicit arg0: scala.reflect.macros.whitebox.Context.WeakTypeTag[T]): scala.reflect.macros.whitebox.Context.Tree
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 callingDerivation.gen[T]
or withimplicitly[Typeclass[T]]
, if the implicit method is imported into the current scope.If the
gen
is notimplicit
, semi-auto derivation is used instead, whereby implicits will not be generated outside of this ADT.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 likeShow[T]
which take only a single type parameter, Magnolia can also derive typeclass instances for types such asDecoder[Format, Type]
which would typically fix theFormat
parameter while varying theType
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 calledcombine
on the derivation object, which may be called with the code above, and for it to return a type which conforms to the typeTypeclass[T]
. The implementation ofcombine
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 todispatch
as tocombine
. - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()