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

Change scope of ClassTagWrapper in NewType to public? #1253

Open
tim-2022 opened this issue Feb 16, 2024 · 0 comments
Open

Change scope of ClassTagWrapper in NewType to public? #1253

tim-2022 opened this issue Feb 16, 2024 · 0 comments

Comments

@tim-2022
Copy link
Contributor

tim-2022 commented Feb 16, 2024

When attempting to use Magnolia to generate macros which rely upon "ClassTag" (see below for a simplified example), Magnolia complains that the ClassTagWrapper helper isn't accessible:

object ClassTagWrapper in object Newtype cannot be accessed as a member of object zio.prelude.Newtype from object Part

I've tried using a local copy of ClassTagWrapper, but that doesn't seem to fix the problem, either, so I can't see another alternative than to give up on code reuse (I could manually define tags() on every Subtype we have, but there are many) or open up the scope of ClassTagWrapper. (Otherwise, let me know if you think I'm overlooking something!)

Example (also found at Scastie at https://scastie.scala-lang.org/OTm2XTJNTeafBu4I58HCtA).

import zio.prelude._
import scala.reflect.ClassTag
import scala.language.experimental.macros
import magnolia1.{CaseClass, Magnolia}

case class Part(name: PartId)

object Part {
  implicit val tags: Tags[Part] = Tags.forClass[Part]
}

type PartId = PartId.Type
object PartId extends Subtype[Int] with SubtypeTags[Int]

trait SubtypeTags[A] { this: Subtype[A] =>
  implicit def tags(implicit tag: ClassTag[Type], tags: Tags[A]): Tags[Type] =
    Tags(tag :: tags.tags)
}

case class Tags[A](tags: List[ClassTag[_]])

object Tags {
  type Typeclass[A] = Tags[A]
  def forClass[A]: Tags[A] = macro Magnolia.gen[A]
  def join[A](cc: CaseClass[Tags, A])(implicit t: ClassTag[A]): Tags[A] =
    Tags[A](t :: cc.parameters.flatMap(_.typeclass.tags).toList)
  implicit val forInt: Tags[Int] = Tags(List(implicitly[ClassTag[Int]]))
}

Thanks very much!
Tim

tim-2022 pushed a commit to tim-2022/zio-prelude that referenced this issue Apr 12, 2024
tim-2022 added a commit to tim-2022/zio-prelude that referenced this issue Apr 12, 2024
zio#1253

Solves:

zio#1253

(Error message: "object ClassTagWrapper in object Newtype cannot be
accessed as a member of object zio.prelude.Newtype" when attempting
to use a ClassTag on a Newtype or Subtype.)
guizmaii pushed a commit that referenced this issue Apr 13, 2024
#1253

Solves:

#1253

(Error message: "object ClassTagWrapper in object Newtype cannot be
accessed as a member of object zio.prelude.Newtype" when attempting
to use a ClassTag on a Newtype or Subtype.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant