Eugene Burmako | 1 Aug 2012 10:56
Picon
Gravatar

Invoking a constructor through reflection in scala 2.10

What's the best practice for invoking a constructor of a class in
scala 2.10 (M4+) ?

For example, this is the constructor methodSymbol:
val constructor =
reflect.runtime.universe.typeOf[scala.List[_]].members.find
(_.toString contains "constructor").get.asMethodSymbol

Eugene Burmako | 1 Aug 2012 10:56
Picon
Gravatar

Re: Invoking a constructor through reflection in scala 2.10

import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}

class C

object Test extends App {
  val ctor = typeOf[C].member(nme.CONSTRUCTOR).asMethodSymbol
  val c = typeOf[C].typeSymbol.asClassSymbol
  val mm = cm.reflectClass(c).reflectConstructor(ctor)
  println(mm())
}

On Aug 1, 10:56 am, Eugene Burmako <xeno...@...> wrote:
> What's the best practice for invoking a constructor of a class in
> scala 2.10 (M4+) ?
>
> For example, this is the constructor methodSymbol:
> val constructor =
> reflect.runtime.universe.typeOf[scala.List[_]].members.find
> (_.toString contains "constructor").get.asMethodSymbol

√iktor Ҡlang | 1 Aug 2012 11:01
Picon

Re: Re: Invoking a constructor through reflection in scala 2.10



On Wed, Aug 1, 2012 at 10:56 AM, Eugene Burmako <xeno.by-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}

class C

object Test extends App {
  val ctor = typeOf[C].member(nme.CONSTRUCTOR).asMethodSymbol
  val c = typeOf[C].typeSymbol.asClassSymbol
  val mm = cm.reflectClass(c).reflectConstructor(ctor)
  println(mm())
}

Can we sprinkle some sugar?
 

On Aug 1, 10:56 am, Eugene Burmako <xeno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> What's the best practice for invoking a constructor of a class in
> scala 2.10 (M4+) ?
>
> For example, this is the constructor methodSymbol:
> val constructor =
> reflect.runtime.universe.typeOf[scala.List[_]].members.find
> (_.toString contains "constructor").get.asMethodSymbol



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: <at> viktorklang

Eugene Burmako | 1 Aug 2012 11:27
Picon
Gravatar

Re: Invoking a constructor through reflection in scala 2.10

It's unclear exactly what sugar to introduce.

Back then we discussed two different directions:
1) Convenience methods like `Type.methods`, `method(String)`,
`fields`, `field(String)` and so on
2) Shortcuts like `mirror.reflectMethod(name)` and possibly
`reflectMethod(name, types)` (see more details in [1])

However both of them are heavyweight. E.g. #1 is annoying to implement
(you need to introduce convenience lookup methods for fields, methods,
ctors, type members, classes, modules - ugh!). #2 would also take a
lot of implementation effort to mirror symbol taxonomy on the mirror
level.

Hence (mostly because of time pressure) for 2.10.0-final we decided to
go with a barebones API and figure out the conveniences later.

[1] https://github.com/scala/scala/blob/master/src/reflect/scala/reflect/api/Mirrors.scala

On Aug 1, 11:01 am, √iktor Ҡlang <viktor.kl...@...> wrote:
> On Wed, Aug 1, 2012 at 10:56 AM, Eugene Burmako <xeno...@...> wrote:
> > import scala.reflect.runtime.universe._
> > import scala.reflect.runtime.{currentMirror => cm}
>
> > class C
>
> > object Test extends App {
> >   val ctor = typeOf[C].member(nme.CONSTRUCTOR).asMethodSymbol
> >   val c = typeOf[C].typeSymbol.asClassSymbol
> >   val mm = cm.reflectClass(c).reflectConstructor(ctor)
> >   println(mm())
> > }
>
> Can we sprinkle some sugar?
>
>
>
> > On Aug 1, 10:56 am, Eugene Burmako <xeno...@...> wrote:
> > > What's the best practice for invoking a constructor of a class in
> > > scala 2.10 (M4+) ?
>
> > > For example, this is the constructor methodSymbol:
> > > val constructor =
> > > reflect.runtime.universe.typeOf[scala.List[_]].members.find
> > > (_.toString contains "constructor").get.asMethodSymbol
>
> --
> Viktor Klang
>
> Akka Tech Lead
> Typesafe <http://www.typesafe.com/> - The software stack for applications
> that scale
>
> Twitter:  <at> viktorklang


Gmane