dynamic instantiation

View: New views
5 Messages — Rating Filter:   Alert me  

dynamic instantiation

by noespam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

How to instantiate a class and method dynamically?

class Foo {
  def index():String = "inside index"
}

I looked on StackOverflow and the suggestion for Scala 2.8 is:

import reflect.Invocation._

val s:String = (new Foo) oo 'index

Seems to work, but I don't know the class ahead of time. I have a substring "Foo" which is parsed from a longer string.

Re: [scala] dynamic instantiation

by Randall Schulz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday October 20 2009, noespam wrote:

> How to instantiate a class and method dynamically?
>
> class Foo {
>   def index():String = "inside index"
> }
>
> I looked on StackOverflow and the suggestion for Scala 2.8 is:
>
> import reflect.Invocation._
>
> val s:String = (new Foo) oo 'index
>
> Seems to work, but I don't know the class ahead of time. I have a
> substring "Foo" which is parsed from a longer string.

To instantiate a class for which you have a (fully qualified) name, do
this:

    Class.forName("pkg.Foo").newInstance

... assuming Foo has a zero-arg constructor. If not, you have to pick
one from the return value from getConstructors() or find a specific one
using getConstructor() passing an argument signature for which a
constructor is defined and then invoke that Constructor with suitable
arguments.


Randall Schulz
--
"I fear dynamic languages have forced us
 to be less creative than we really are."

(with apologies to Lily Tomlin)

Re: [scala] dynamic instantiation

by Arthur Peters :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Not to butt in but it should be pointed out that this is exactly the same as Java. There is no Scala specific API for this so just look for Java reflection documentation and you will find out how to do this stuff. You can then just call the Java reflection methods from Scala easily.

-Arthur


On Tue, Oct 20, 2009 at 11:33 PM, Randall R Schulz <rschulz@...> wrote:
On Tuesday October 20 2009, noespam wrote:
> How to instantiate a class and method dynamically?
>
> class Foo {
>   def index():String = "inside index"
> }
>
> I looked on StackOverflow and the suggestion for Scala 2.8 is:
>
> import reflect.Invocation._
>
> val s:String = (new Foo) oo 'index
>
> Seems to work, but I don't know the class ahead of time. I have a
> substring "Foo" which is parsed from a longer string.

To instantiate a class for which you have a (fully qualified) name, do
this:

   Class.forName("pkg.Foo").newInstance

... assuming Foo has a zero-arg constructor. If not, you have to pick
one from the return value from getConstructors() or find a specific one
using getConstructor() passing an argument signature for which a
constructor is defined and then invoke that Constructor with suitable
arguments.


Randall Schulz
--
"I fear dynamic languages have forced us
 to be less creative than we really are."

(with apologies to Lily Tomlin)


Re: [scala] dynamic instantiation

by noespam :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for confirming. I wanted to avoid doing it the Java way and hoped an undocumented feature made its way into 2.8.

Arthur Peters wrote:
Not to butt in but it should be pointed out that this is exactly the same as
Java. There is no Scala specific API for this so just look for Java
reflection documentation and you will find out how to do this stuff. You can
then just call the Java reflection methods from Scala easily.

-Arthur


On Tue, Oct 20, 2009 at 11:33 PM, Randall R Schulz <rschulz@sonic.net>wrote:

> On Tuesday October 20 2009, noespam wrote:
> > How to instantiate a class and method dynamically?
> >
> > class Foo {
> >   def index():String = "inside index"
> > }
> >
> > I looked on StackOverflow and the suggestion for Scala 2.8 is:
> >
> > import reflect.Invocation._
> >
> > val s:String = (new Foo) oo 'index
> >
> > Seems to work, but I don't know the class ahead of time. I have a
> > substring "Foo" which is parsed from a longer string.
>
> To instantiate a class for which you have a (fully qualified) name, do
> this:
>
>    Class.forName("pkg.Foo").newInstance
>
> ... assuming Foo has a zero-arg constructor. If not, you have to pick
> one from the return value from getConstructors() or find a specific one
> using getConstructor() passing an argument signature for which a
> constructor is defined and then invoke that Constructor with suitable
> arguments.
>
>
> Randall Schulz
> --
> "I fear dynamic languages have forced us
>  to be less creative than we really are."
>
> (with apologies to Lily Tomlin)
>

[scala] Re: dynamic instantiation

by Martin Kneissl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've written some sketches of how to instantiate objects via reflection
in a Scala earlier this year. http://www.familie-kneissl.org/Members/
martin/blog/reflection-from-scala-heaven-and-hell

This predates 2.8, though. Some ideas have been taken to
scala.reflect.Invocation, but the New mechanism hasn't.

Martin.

On Wed, 21 Oct 2009 00:06:42 -0700 noespam wrote:

> Thanks for confirming. I wanted to avoid doing it the Java way and hoped
> an undocumented feature made its way into 2.8.
>
>
> Arthur Peters wrote:
>>
>> Not to butt in but it should be pointed out that this is exactly the
>> same as
>> Java. There is no Scala specific API for this so just look for Java
>> reflection documentation and you will find out how to do this stuff.
>> You can
>> then just call the Java reflection methods from Scala easily.
>>
>> -Arthur
>>
>>
>> On Tue, Oct 20, 2009 at 11:33 PM, Randall R Schulz
>> <rschulz@...>wrote:
>>
>>> On Tuesday October 20 2009, noespam wrote:
>>> > How to instantiate a class and method dynamically?
>>> >
>>> > class Foo {
>>> >   def index():String = "inside index"
>>> > }
>>> >
>>> > I looked on StackOverflow and the suggestion for Scala 2.8 is:
>>> >
>>> > import reflect.Invocation._
>>> >
>>> > val s:String = (new Foo) oo 'index
>>> >
>>> > Seems to work, but I don't know the class ahead of time. I have a
>>> > substring "Foo" which is parsed from a longer string.
>>>
>>> To instantiate a class for which you have a (fully qualified) name, do
>>> this:
>>>
>>>    Class.forName("pkg.Foo").newInstance
>>>
>>> ... assuming Foo has a zero-arg constructor. If not, you have to pick
>>> one from the return value from getConstructors() or find a specific
>>> one using getConstructor() passing an argument signature for which a
>>> constructor is defined and then invoke that Constructor with suitable
>>> arguments.
>>>
>>>
>>> Randall Schulz
>>> --
>>> "I fear dynamic languages have forced us
>>>  to be less creative than we really are."
>>>
>>> (with apologies to Lily Tomlin)
>>>
>>>
>>