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

Converting a String/Name into a Type

2 replies
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Howdy.

I'm struggling through the compiler code.  I'm at a loss as to how to convert a string into a Type.  For example, if I have "net.liftweb.util.Helpers", how do I get a Type instance the represents the class (assuming that a JAR containing the class is on the classpath)?

Thanks,

David

--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Converting a String/Name into a Type

On Wed, Jun 10, 2009 at 04:04:18PM -0700, David Pollak wrote:
> I'm struggling through the compiler code. I'm at a loss as to how to
> convert a string into a Type. For example, if I have
> "net.liftweb.util.Helpers", how do I get a Type instance the
> represents the class (assuming that a JAR containing the class is on
> the classpath)?

Look at Definitions for examples.

// that is a Symbol
lazy val CharSequenceClass = getClass("java.lang.CharSequence")

// this would be a type
CharSequenceClass.tpe

Of course the usual caveats (about how there might be lots of other
stuff one ought to do) apply.

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Converting a String/Name into a Type


On Wed, Jun 10, 2009 at 4:26 PM, Paul Phillips <paulp@improving.org> wrote:
On Wed, Jun 10, 2009 at 04:04:18PM -0700, David Pollak wrote:
> I'm struggling through the compiler code.  I'm at a loss as to how to
> convert a string into a Type.  For example, if I have
> "net.liftweb.util.Helpers", how do I get a Type instance the
> represents the class (assuming that a JAR containing the class is on
> the classpath)?

Look at Definitions for examples.

 // that is a Symbol
 lazy val CharSequenceClass = getClass("java.lang.CharSequence")


Well crap, I'm an idiot.  I was trying to create a Name and pass a name to getClass... that failed... but the String, the String... it worked.  Sorry for being really frickin' stupid.
 

 // this would be a type
 CharSequenceClass.tpe

Of course the usual caveats (about how there might be lots of other
stuff one ought to do) apply.

--
Paul Phillips      | A national political campaign is better than the
Everyman           | best circus ever heard of, with a mass baptism and
Empiricist         | a couple of hangings thrown in.
i pull his palp!   |     -- H. L. Mencken



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

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