Classes , imports, etc.

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Classes , imports, etc.

Russel Winder-3
This probably seems like a "Dear lazyweb," but if anyone does have any
thoughts that would be great…

It strikes me that a dynamic language on the JVM should be able to do
something along the lines of:

{'a.b.c', 'f.y.z.r', 'd.d.d'}.each{
        import it
        def x = new it()
        …
}

But I am beginning the thing that this is not possible in Groovy (or
Java, JRuby, Clojure, Scala?) whereas it is possible on the PVM with
Python. Mayhap Jython can, which means mayhap JRuby and Clojure can?

--
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:[hidden email]
41 Buckmaster Road    m: +44 7770 465 077   xmpp: [hidden email]
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Classes , imports, etc.

Andrew Eisenberg
No.  Not possible in groovy, or at least would require some fundamental re-writing of the compiler and runtime.  The imports are required for compiling and linking class files.  All referenced class files are known at compile time.  In theory, things could be rewritten so that they are not really "import" statements, and compile down to some sort of Class.forName("a.b.c.d") expression.  But, if they are not really import statements, then you don't get any of the benefits of compile time checking and it is no different from Class.forName("a.b.c.d").newInstance() (with a nicer syntax).

Do you have a real use case for this or are you just musing?


On Sun, Apr 21, 2013 at 6:30 AM, Russel Winder <[hidden email]> wrote:
This probably seems like a "Dear lazyweb," but if anyone does have any
thoughts that would be great…

It strikes me that a dynamic language on the JVM should be able to do
something along the lines of:

{'a.b.c', 'f.y.z.r', 'd.d.d'}.each{
        import it
        def x = new it()
        …
}

But I am beginning the thing that this is not possible in Groovy (or
Java, JRuby, Clojure, Scala?) whereas it is possible on the PVM with
Python. Mayhap Jython can, which means mayhap JRuby and Clojure can?

--
Russel.
=============================================================================
Dr Russel Winder      t: <a href="tel:%2B44%2020%207585%202200" value="+442075852200">+44 20 7585 2200   voip: [hidden email]
41 Buckmaster Road    m: <a href="tel:%2B44%207770%20465%20077" value="+447770465077">+44 7770 465 077   xmpp: [hidden email]
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

Reply | Threaded
Open this post in threaded view
|

Re: Classes , imports, etc.

Russel Winder-3
On Sun, 2013-04-21 at 08:16 -0700, Andrew Eisenberg wrote:
> No.  Not possible in groovy, or at least would require some fundamental
> re-writing of the compiler and runtime.  The imports are required for
> compiling and linking class files.  All referenced class files are known at
> compile time.  In theory, things could be rewritten so that they are not
> really "import" statements, and compile down to some sort of
> Class.forName("a.b.c.d") expression.  But, if they are not really import
> statements, then you don't get any of the benefits of compile time checking
> and it is no different from Class.forName("a.b.c.d").newInstance() (with a
> nicer syntax).

I'll just have to find a way with forName.  Thanks for the confirmation,
much appreciated.

> Do you have a real use case for this or are you just musing?

Setting up benchmarking code.  Single driver, multiple classes. I want
to pick up the classes by introspection on the filestore.  This is all
quite easy in Python, I was hoping it would be easy in Groovy. I will
find a way that is not disgusting, just not as nice.

Thanks again.
--
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:[hidden email]
41 Buckmaster Road    m: +44 7770 465 077   xmpp: [hidden email]
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Classes , imports, etc.

Cédric Champeau
I'm not sure this would satisfy your needs, but have you thought of --configscript? You could have a configuration script that does:

withConfig(configuration) {
    ['a.b.c', 'f.y.z.r', 'd.d.d'].each { name ->
        imports { normal(name) }
    }
}

Then run your script with:

groovy --configscript config.groovy yourcode.groovy

See http://docs.codehaus.org/display/GROOVY/Advanced+compiler+configuration for details.

Le 21/04/2013 19:11, Russel Winder a écrit :
On Sun, 2013-04-21 at 08:16 -0700, Andrew Eisenberg wrote:
No.  Not possible in groovy, or at least would require some fundamental
re-writing of the compiler and runtime.  The imports are required for
compiling and linking class files.  All referenced class files are known at
compile time.  In theory, things could be rewritten so that they are not
really "import" statements, and compile down to some sort of
Class.forName("a.b.c.d") expression.  But, if they are not really import
statements, then you don't get any of the benefits of compile time checking
and it is no different from Class.forName("a.b.c.d").newInstance() (with a
nicer syntax).
I'll just have to find a way with forName.  Thanks for the confirmation,
much appreciated.

Do you have a real use case for this or are you just musing?
Setting up benchmarking code.  Single driver, multiple classes. I want
to pick up the classes by introspection on the filestore.  This is all
quite easy in Python, I was hoping it would be easy in Groovy. I will
find a way that is not disgusting, just not as nice.

Thanks again.


-- 
Cédric Champeau
SpringSource - A Division Of VMware
http://www.springsource.com/
http://twitter.com/CedricChampeau
Reply | Threaded
Open this post in threaded view
|

Re: Classes , imports, etc.

Guillaume Laforge-4
In reply to this post by Russel Winder-3
You could always do the following inside your loop:

def x = (it as Class).newInstance()

Guillaume


On Sun, Apr 21, 2013 at 3:30 PM, Russel Winder <[hidden email]> wrote:
This probably seems like a "Dear lazyweb," but if anyone does have any
thoughts that would be great…

It strikes me that a dynamic language on the JVM should be able to do
something along the lines of:

{'a.b.c', 'f.y.z.r', 'd.d.d'}.each{
        import it
        def x = new it()
        …
}

But I am beginning the thing that this is not possible in Groovy (or
Java, JRuby, Clojure, Scala?) whereas it is possible on the PVM with
Python. Mayhap Jython can, which means mayhap JRuby and Clojure can?

--
Russel.
=============================================================================
Dr Russel Winder      t: <a href="tel:%2B44%2020%207585%202200" value="+442075852200">+44 20 7585 2200   voip: [hidden email]
41 Buckmaster Road    m: <a href="tel:%2B44%207770%20465%20077" value="+447770465077">+44 7770 465 077   xmpp: [hidden email]
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one