This page is no longer maintained — Please continue to the home page at www.scala-lang.org

a recent interpreter session

3 replies
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

Who wants structural type inference! If you raised your hand you are
very sick. And yet..

scala> typify(classOf[List[_]], classOf[java.util.List[_]])
res0: String =
{
def hashCode(): Int
def equals(p1: AnyRef): Boolean
def indexOf(p1: AnyRef): Int
def lastIndexOf(p1: AnyRef): Int
def contains(p1: AnyRef): Boolean
def size(): Int
def isEmpty(): Boolean
}

scala> mkType("AnyList", res0)
defined type alias AnyList
res1: scala.tools.nsc.InterpreterResults.Result = Success

scala> var x: AnyList = List("a", "b").asInstanceOf[AnyList]
x: AnyList = List(a, b)

scala> x contains "a"
res2: Boolean = true

scala> x = new java.util.ArrayList[String]()
x: AnyList = []

scala> x.isEmpty
res3: Boolean = true

Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
Re: a recent interpreter session
Very cool....

Does that work in regular Scala programs or only in the interpreter?

On Wed, May 27, 2009 at 6:03 PM, Paul Phillips <paulp [at] improving [dot] org> wrote:
Who wants structural type inference! If you raised your hand you are
very sick.  And yet..

scala> typify(classOf[List[_]], classOf[java.util.List[_]])
res0: String =
{
 def hashCode(): Int
 def equals(p1: AnyRef): Boolean
 def indexOf(p1: AnyRef): Int
 def lastIndexOf(p1: AnyRef): Int
 def contains(p1: AnyRef): Boolean
 def size(): Int
 def isEmpty(): Boolean
}

scala> mkType("AnyList", res0)
defined type alias AnyList
res1: scala.tools.nsc.InterpreterResults.Result = Success

scala> var x: AnyList = List("a", "b").asInstanceOf[AnyList]
x: AnyList = List(a, b)

scala> x contains "a"
res2: Boolean = true

scala> x = new java.util.ArrayList[String]()
x: AnyList = []

scala> x.isEmpty
res3: Boolean = true

--
Paul Phillips      | We must respect the other fellow's religion, but only
Apatheist          | in the sense and to the extent that we respect his
Empiricist         | theory that his wife is beautiful and his children smart.
ha! spill, pupil   |     -- H. L. Mencken



--
http://erikengbrecht.blogspot.com/
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: a recent interpreter session

On Wed, May 27, 2009 at 06:10:32PM -0400, Erik Engbrecht wrote:
> Does that work in regular Scala programs or only in the interpreter?

Scala does not offer any direct mechanism for making a type out of a
string, so everything in my bag of tricks will require either the
interpreter or staged compilation. So it is faked, but it's plausible
it could be faked so well you'd forget what was really happening.

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: a recent interpreter session

On Wednesday May 27 2009, Paul Phillips wrote:
> On Wed, May 27, 2009 at 06:10:32PM -0400, Erik Engbrecht wrote:
> > Does that work in regular Scala programs or only in the
> > interpreter?
>
> Scala does not offer any direct mechanism for making a type out of a
> string, so everything in my bag of tricks will require either the
> interpreter or staged compilation. So it is faked, but it's
> plausible it could be faked so well you'd forget what was really
> happening.

Any sufficiently advanced technology is indistinguishable from magic.
Or a rigged demo.

RRS

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland