Skip to content

Instantly share code, notes, and snippets.

@mandubian
Created April 30, 2014 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandubian/807961eb9a8911f45894 to your computer and use it in GitHub Desktop.
Save mandubian/807961eb9a8911f45894 to your computer and use it in GitHub Desktop.
I can't create a type tree from this and typecheck it... it infers either Any or doesn't typecheck at all... Any workaround?
scala> case class F[M[_], T]()
scala> val tpe = typeOf[F[M, _] forSome { type M[_] }]
##### FIRST TRY
scala> val e = q"{ type Tata[T] = ({ type l[T] = $tpe[scala.concurrent.Future, T] })#l[T]; () }"
e: toolbox.u.Tree =
{
type Tata[T] = scala.AnyRef {
type l[T] = F[_[_] <: Any, _][scala.concurrent.Future, T]
}#l[T];
()
}
scala> toolbox.typecheck(e)
scala.tools.reflect.ToolBoxError: reflective typecheck has failed: Any does not take type parameters
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$typecheck$1.apply(ToolBoxFactory.scala:177)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$typecheck$1.apply(ToolBoxFactory.scala:169)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$11.apply(ToolBoxFactory.scala:147)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$11.apply(ToolBoxFactory.scala:147)
at scala.tools.nsc.typechecker.Contexts$Context.withMode(Contexts.scala:374)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$9.apply(ToolBoxFactory.scala:138)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$9.apply(ToolBoxFactory.scala:138)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$withContext$1$1.apply(ToolBoxFactory.scala:139)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$withContext$1$1.apply(ToolBoxFactory.scala:139)
at scala.tools.nsc.typechecker.Contexts$Context.withMode(Contexts.scala:374)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$7.apply(ToolBoxFactory.scala:137)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1$$anonfun$7.apply(ToolBoxFactory.scala:137)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1.apply(ToolBoxFactory.scala:147)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$transformDuringTyper$1.apply(ToolBoxFactory.scala:121)
at scala.reflect.internal.Trees$class.wrappingIntoTerm(Trees.scala:1691)
at scala.reflect.internal.SymbolTable.wrappingIntoTerm(SymbolTable.scala:16)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.withWrapping$1(ToolBoxFactory.scala:120)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.transformDuringTyper(ToolBoxFactory.scala:121)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.typecheck(ToolBoxFactory.scala:168)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$typecheck$2.apply(ToolBoxFactory.scala:374)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$typecheck$2.apply(ToolBoxFactory.scala:366)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.liftedTree2$1(ToolBoxFactory.scala:354)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.apply(ToolBoxFactory.scala:354)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:366)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:27)
... 32 elided
##### SECOND TRY
scala> val n = tpe.typeConstructor.normalize
scala> PolyType(params, raw) = n
scala> val subst = raw.substituteTypes(List(params(0)), List(typeOf[scala.concurrent.Future[_]].typeConstructor))
subst: toolbox.u.Type = F[scala.concurrent.Future,T]
scala> val e = q"{ type Tata[T] = ({ type l[T] = $subst })#l[T]; () }"
e: toolbox.u.Tree =
{
type Tata[T] = scala.AnyRef {
type l[T] = F[scala.concurrent.Future,T]
}#l[T];
()
}
scala> toolbox.typecheck(e)
scala.MatchError: <tree with deferred refcheck> (of class scala.tools.nsc.ast.Trees$TypeTreeWithDeferredRefCheck)
at scala.reflect.internal.Importers$StandardImporter.recreateTree(Importers.scala:309)
at scala.reflect.internal.Importers$StandardImporter.importTree(Importers.scala:415)
at scala.reflect.internal.Importers$StandardImporter.recreateTree(Importers.scala:392)
at scala.reflect.internal.Importers$StandardImporter.importTree(Importers.scala:415)
at scala.reflect.internal.Importers$StandardImporter.recreateTree(Importers.scala:325)
at scala.reflect.internal.Importers$StandardImporter.importTree(Importers.scala:415)
at scala.reflect.internal.Importers$StandardImporter$$anonfun$recreateTree$10.apply(Importers.scala:333)
at scala.reflect.internal.Importers$StandardImporter$$anonfun$recreateTree$10.apply(Importers.scala:333)
at scala.collection.immutable.List.map(List.scala:274)
at scala.reflect.internal.Importers$StandardImporter.recreateTree(Importers.scala:333)
at scala.reflect.internal.Importers$StandardImporter.importTree(Importers.scala:415)
at scala.reflect.internal.Importers$StandardImporter.importTree(Importers.scala:29)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$typecheck$2.apply(ToolBoxFactory.scala:375)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$typecheck$2.apply(ToolBoxFactory.scala:366)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.liftedTree2$1(ToolBoxFactory.scala:354)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.apply(ToolBoxFactory.scala:354)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:366)
at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.typecheck(ToolBoxFactory.scala:27)
... 32 elided
@metasim
Copy link

metasim commented Aug 27, 2015

@mandubian Did you figure out a solution to the first case? I'm running into the same problem (but with Double instead of Any).

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