Message from discussion
ToolBox(Factory).inferImplicit View or alternatives?
Received: by 10.50.94.166 with SMTP id dd6mr3590454igb.3.1340453375165;
Sat, 23 Jun 2012 05:09:35 -0700 (PDT)
X-BeenThere: scala-internals@googlegroups.com
Received: by 10.50.178.73 with SMTP id cw9ls495015igc.4.canary; Sat, 23 Jun
2012 05:09:33 -0700 (PDT)
Received: by 10.50.160.167 with SMTP id xl7mr3587939igb.4.1340453373906;
Sat, 23 Jun 2012 05:09:33 -0700 (PDT)
Received: by 10.50.160.167 with SMTP id xl7mr3587937igb.4.1340453373897;
Sat, 23 Jun 2012 05:09:33 -0700 (PDT)
Return-Path: <mi...@milessabin.com>
Received: from mail-ob0-f181.google.com (mail-ob0-f181.google.com [209.85.214.181])
by gmr-mx.google.com with ESMTPS id bg10si814065igc.3.2012.06.23.05.09.33
(version=TLSv1/SSLv3 cipher=OTHER);
Sat, 23 Jun 2012 05:09:33 -0700 (PDT)
Received-SPF: pass (google.com: domain of mi...@milessabin.com designates 209.85.214.181 as permitted sender) client-ip=209.85.214.181;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of mi...@milessabin.com designates 209.85.214.181 as permitted sender) smtp.mail=mi...@milessabin.com
Received: by obbup19 with SMTP id up19so3938441obb.26
for <scala-internals@googlegroups.com>; Sat, 23 Jun 2012 05:09:33 -0700 (PDT)
d=google.com; s=20120113;
h=mime-version:in-reply-to:references:from:date:message-id:subject:to
:content-type:content-transfer-encoding:x-gm-message-state;
bh=itREbzO+p26w66bCQcpdGjlirX6xxG9PZjGkOZD6D8Q=;
b=giXSiYBmQoacgKIuG1q2SEFWIht7XLm3c5130iYE/sF1B17Q0Fyf2rDGpFerRfgjZO
PQTehkE+MPil9Lzzx6hfpcT4zj/JY/tI6fWNNA+4RhuWjxqdrtKZZUqHeTa1bzNsUsoW
7oLfM140PfXRvz4R7j73X0YsVoXC2vAVYZnkfZjxSgJmeLnTSwaW5fiZ4otQDAa51euX
UpFDiBMf8bhUh3dMVJ1ekzt2kuVE6MY6RCozZB/eR6iphpNboAhQfNAyigJ6kEZwc/VM
H4mgr4CBMBwO0Lc4f1Oklawrr17hebS4MfX3LhjlB0l8OTwmh/vbWIJ4xQZX8RDgLb/B
AwhA==
Received: by 10.60.9.134 with SMTP id z6mr5540405oea.46.1340453373334; Sat, 23
Jun 2012 05:09:33 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.182.86.197 with HTTP; Sat, 23 Jun 2012 05:09:03 -0700 (PDT)
In-Reply-To: <CAAtns_CycT2iC5H854jRF6vBk-TknVgQyaPVjQBx7NpTyxW...@mail.gmail.com>
References: <CAAtns_Bt40EEe4M5yv2Zpdyvgm+0Yh63hFF398XnEsbwFBw...@mail.gmail.com>
<CAA2kKAuUDKPWdYEsCLC=D1EVj50HG=Qr8LxTvF-6kgd=Ljb...@mail.gmail.com>
<CAAtns_BXXAezrbUrCGtS15+ptnq5YbtB1Ntf3k3dhmCgqss...@mail.gmail.com> <CAAtns_CycT2iC5H854jRF6vBk-TknVgQyaPVjQBx7NpTyxW...@mail.gmail.com>
From: Miles Sabin <mi...@milessabin.com>
Date: Sat, 23 Jun 2012 13:09:03 +0100
Message-ID: <CAAtns_ATi6FTN7V0kTD8BevMtersw8XtetC9wL5WgGREXpU...@mail.gmail.com>
Subject: Re: [scala-internals] ToolBox(Factory).inferImplicitView or alternatives?
To: scala-internals@googlegroups.com
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Gm-Message-State: ALoCoQlYbixwCJ7w5TWbumqtwDsTS/K5ywIg0J5E++X5Qklbz4UoAfFOAj+6ms5Z8FyT2ZXixIak
I wrote,
> compiles and produces,
>
> TypeApply(Select(Select(This(newTypeName("foo")), foo.Foo),
> =A0newTermName("mkFoo")), List(TypeTree(), TypeTree()))
>
> which looks like it's getting pretty much to where I want it to be :-)
Unfortunately if I then try and run this tree,
val ivb =3D toolbox.resetAllAttrs(iv)
val v =3D toolbox.runExpr(ivb)
println(v)
I get the following error at runtime,
scala.tools.reflect.ToolBoxError: reflective toolbox has failed:
cannot operate on trees that are already typed
Is there some other incantation necessary to compile a tree that's
been inferred in this way?
Alternatively, I'd actually prefer to be able to do this in a single
step rather than infer the implicit and then evaluate it. I've tried
things along the lines of,
package foo {
trait Foo[T, U]
object Foo {
implicit val is =3D new Foo[Int, String] {
override def toString() =3D "Foo[Int, String]"
}
}
}
package bar {
object Test extends App {
import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
val toolbox =3D currentMirror.mkToolBox()
val tree =3D
Apply(
TypeApply(
Select(
Select(
Ident(newTermName("scala")),
newTermName("Predef")),
newTermName("implicitly")),
List(
AppliedTypeTree(
Select(
Ident(newTermName("foo")),
newTypeName("Foo")),
List(
Select(
Ident(newTermName("scala")),
newTypeName("Int")),
Select(
Select(
Ident(newTermName("java")),
newTermName("lang")),
newTypeName("String")))))), Nil)
val rv =3D toolbox.runExpr(tree)
println(rv)
}
}
Here I get a runtime error,
scala.tools.reflect.ToolBoxError: reflective compilation has failed:
not enough arguments for method implicitly: (implicit e:
foo.Foo[Int,String])foo.Foo[Int,String].
Unspecified value parameter e.
So it looks as though in the inferImplicitValue case typechecking goes
too far to enable subsequent compilation and execution, and in the
second case it doesn't do enough work to resolve the implicit
arguments. Any ideas?
Cheers,
Miles
--=20
Miles Sabin
tel: +44 7813 944 528
gtalk: mi...@milessabin.com
skype: milessabin
g+: http://www.milessabin.com
http://twitter.com/milessabin
http://underscoreconsulting.com
http://www.chuusai.com