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

No TypeTag available for newtypes #58

Open
LMnet opened this issue Feb 12, 2020 · 2 comments
Open

No TypeTag available for newtypes #58

LMnet opened this issue Feb 12, 2020 · 2 comments

Comments

@LMnet
Copy link

LMnet commented Feb 12, 2020

Currently, there is no way to get a TypeTag for a newtype:

@newtype case class WidgetId(toInt: Int)

val widgetId = WidgetId(5)

import scala.reflect.runtime.universe._

val tt = typeTag[WidgetId] // failed with No TypeTag available

WeakTypeTag works fine:

val wtt = weakTypeTag[WidgetId]

Also, I can get a TypeTag for the Repr type:

val repr = typeTag[WidgetId.Repr]

Looks like there is no way to get a TypeTag for the newtype in the current newtype encoding because newtype's type is abstract, but TypeTag can be summoned only for a concrete type.

About my use case: I'm using doobie and it uses TypeTags very extensively for better logging and error reporting.

@carymrobbins
Copy link
Member

It seems one workaround would be to use Coercible (special thanks to @hmemcpy for pointing this out to others)

implicit def coercibleMeta[R, N](implicit ev: Coercible[Meta[R], Meta[N]], R: Meta[R]): Meta[N] = ev(R)

https://twitter.com/hmemcpy/status/1253731339825790976?s=19

@hmemcpy
Copy link

hmemcpy commented Apr 28, 2020

Yeah, unfortunately I couldn't get the tmap/tcontramap variant working, so this workaround uses the regular map/contramap on doobie types. This works, but as @LMnet mentions, won't provide decent logging output when things go wrong.

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

3 participants