Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the way to use only dispatch except combine safely #258

Closed
taeguk opened this issue May 26, 2020 · 0 comments · Fixed by #263
Closed

Support the way to use only dispatch except combine safely #258

taeguk opened this issue May 26, 2020 · 0 comments · Fixed by #263
Assignees

Comments

@taeguk
Copy link

taeguk commented May 26, 2020

This is small example for showing what I want to say:

import language.experimental.macros
import magnolia._
import cats.Show

object ShowDerivation {
  type Typeclass[T] = Show[T]

  def combine[T](ctx: CaseClass[Show, T]): Show[T] = new Show[T] {
    def show(value: T): String =
      throw new Exception("Don't support combine for show derivation.")
  }

  def dispatch[T](ctx: SealedTrait[Show, T]): Show[T] = new Show[T] {
    def show(value: T): String =
      ctx.dispatch(value)(sub => sub.typeclass.show(sub.cast(value)))
  }

  def gen[T]: Show[T] = macro Magnolia.gen[T]
}

sealed trait MySealedTrait
case class MyCaseClass(a: Int) extends MySealedTrait
case object MyCaseObject       extends MySealedTrait

// There is the compile error:
// magnolia: could not find Show.Typeclass for type Int
//     in parameter 'a' of product type ForFun.MyCaseClass
val a = ShowDerivation.gen[MyCaseClass]

// There is no compile error.
// I want to make a compilation fails here.
val b = ShowDerivation.gen[MyCaseObject.type]

Currently, I use magnolia for an automatic materialization of sealed trait when materializations of derived types (case class and case object) are given manually.
In this usage of magnolia, I only need dispatch except combine. And I want to make a compilation fails if there is no materialization of any derived type to prevent mistakes.

I think it can be helpful in many cases to support the way to use only dispatch except combine safely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants