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

Stop @newtype to warn for implicit conversions #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.estatico.newtype.macros

import scala.reflect.macros.blackbox

private[macros] trait ExtraDefs {
val c: blackbox.Context

import c.universe._

val enableHKTs: List[Tree] = List( q"import _root_.scala.language.higherKinds" )

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.estatico.newtype.macros

import scala.reflect.macros.blackbox

private[macros] trait ExtraDefs {
val c: blackbox.Context

import c.universe._

val enableHKTs: List[Tree] = List( q"import _root_.scala.language.higherKinds" )

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.estatico.newtype.macros

import scala.reflect.macros.blackbox

private[macros] trait ExtraDefs {
val c: blackbox.Context

import c.universe._

val enableHKTs: List[Tree] = Nil

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.estatico.newtype.Coercible
import scala.reflect.ClassTag
import scala.reflect.macros.blackbox

private[macros] class NewTypeMacros(val c: blackbox.Context) {
private[macros] class NewTypeMacros(val c: blackbox.Context) extends ExtraDefs {

import c.universe._

Expand Down Expand Up @@ -109,10 +109,11 @@ private[macros] class NewTypeMacros(val c: blackbox.Context) {
val reprType = valDef.tpt
val typesTraitName = TypeName(s"${clsName.decodedName}__Types")
val tparams = clsDef.tparams
val maybeOps = maybeGenerateOpsDef(clsDef, valDef, tparamsNoVar, tparamNames)
val companionExtraDefs =
maybeGenerateApplyMethod(clsDef, valDef, tparamsNoVar, tparamNames) :::
maybeGenerateUnapplyMethod(clsDef, valDef, tparamsNoVar, tparamNames) :::
maybeGenerateOpsDef(clsDef, valDef, tparamsNoVar, tparamNames) :::
maybeOps :::
generateCoercibleInstances(tparamsNoVar, tparamNames, tparamsWild) :::
generateDerivingMethods(tparamsNoVar, tparamNames, tparamsWild)

Expand All @@ -123,9 +124,14 @@ private[macros] class NewTypeMacros(val c: blackbox.Context) {
val baseTypeDef = mkBaseTypeDef(clsDef, reprType, subtype)
val typeTypeDef = mkTypeTypeDef(clsDef, tparamNames, subtype)

val enableImplicits: List[Tree] = List( q"import _root_.scala.language.implicitConversions" )

val imports: List[Tree] =
(if (maybeOps.isEmpty) Nil else enableImplicits) ++ enableHKTs

val newtypeObjParents = objParents
val newtypeObjDef = ModuleDef(
objMods, objName, Template(newtypeObjParents, objSelf, objDefs ++ companionExtraDefs ++ Seq(
objMods, objName, Template(newtypeObjParents, objSelf, objDefs ++ imports ++ companionExtraDefs ++ Seq(
q"type Repr[..$tparams] = $reprType",
baseTypeDef,
q"trait Tag[..$tparams] extends _root_.scala.Any",
Expand Down