What happens to 'def' attributes in scripts?

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

What happens to 'def' attributes in scripts?

by Niels B Nielsen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi,

 

When I have my users define their own script, they produce something similar to this:

 

Test.groovy

--------------

n=1

m=2

 

def add(another) {

  int x = n+another

  println "x is ${x}"

}

add(m)

-------------------------

The def method is added to Test.class, and the variables are added to the Binding. That much I have verified, but when the users sometimes write

 

def n=1

def m=2

 

The script fails to recognize n in the add method. (no such property: n for class: Test)

 

Clearly, neither n nor m is a field of class Test, nor are they present in the binding. However, m is still accessible to be an argument to the add method.

 

I can easily tell the user not to specify def in front of variables, only in front of methods, but I cannot fully explain why.

 

Where is the variable stored? In the main method? or just inline replaced during parsing? Hope you can help me with an answer.

 

Regards,

/Niels


This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.


Re: What happens to 'def' attributes in scripts?

by Edward Sumerfield-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Woo, I can answer this one.

When groovyc compiles this script it has to convert into java byte code. So, the n=1 become Test instance attributes, the def n=1 become main method local variables.

The case without def results in java looking something like this:

    class Test
    {
        Object n = 1
        Object m = 2

        public static void main(String args[])
        {
            add(m)
        }

        void add(Object another)
        {
            another += n
        }
    }

this works because there are no defs and the scoping of n and m remain accessible by the add method

However, add the def and they become local variables in the main method and now are not accessible by add.

    class Test
    {
        public static void main(String args[])
        {
            Object n = 1
            Object m = 2

            add(m)
        }

        void add(Object another)
        {
            another += n
        }
    }



On Tue, Jun 30, 2009 at 4:21 PM, Niels B Nielsen <niels.b.nielsen@...> wrote:

Hi,

When I have my users define their own script, they produce something similar to this:

Test.groovy

--------------

n=1

m=2

 

def add(another) {

  int x = n+another

  println "x is ${x}"

}

add(m)

-------------------------

The def method is added to Test.class, and the variables are added to the Binding. That much I have verified, but when the users sometimes write

 

def n=1

def m=2

 

The script fails to recognize n in the add method. (no such property: n for class: Test)

 

Clearly, neither n nor m is a field of class Test, nor are they present in the binding. However, m is still accessible to be an argument to the add method.

 

I can easily tell the user not to specify def in front of variables, only in front of methods, but I cannot fully explain why.

 

Where is the variable stored? In the main method? or just inline replaced during parsing? Hope you can help me with an answer.

 

Regards,

/Niels


This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.



RE: What happens to 'def' attributes in scripts?

by Niels B Nielsen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Great thanks,

 

Just as I expected. That leads me to the following questions..

 

1) Why does methods have to be def'ed.

2) How can I specify the type of my attribute?

 

Some of the users have programming experience and

a) either want def func() and def var to be consistent

b) by mistake write int n=1 very often.

 

It just seems a bit inconsistent to me.

 

Regards,

/Niels

 

 

From: Edward Sumerfield [mailto:esumerfd@...]
Sent: 30 June 2009 21:36
To: user@...
Subject: Re: [groovy-user] What happens to 'def' attributes in scripts?

 

Woo, I can answer this one.

When groovyc compiles this script it has to convert into java byte code. So, the n=1 become Test instance attributes, the def n=1 become main method local variables.

The case without def results in java looking something like this:

    class Test
    {
        Object n = 1
        Object m = 2

        public static void main(String args[])
        {
            add(m)
        }

        void add(Object another)
        {
            another += n
        }
    }

this works because there are no defs and the scoping of n and m remain accessible by the add method

However, add the def and they become local variables in the main method and now are not accessible by add.

    class Test
    {
        public static void main(String args[])
        {
            Object n = 1
            Object m = 2

            add(m)
        }

        void add(Object another)
        {
            another += n
        }
    }


On Tue, Jun 30, 2009 at 4:21 PM, Niels B Nielsen <niels.b.nielsen@...> wrote:

Hi,

When I have my users define their own script, they produce something similar to this:

Test.groovy

--------------

n=1

m=2

 

def add(another) {

  int x = n+another

  println "x is ${x}"

}

add(m)

-------------------------

The def method is added to Test.class, and the variables are added to the Binding. That much I have verified, but when the users sometimes write

 

def n=1

def m=2

 

The script fails to recognize n in the add method. (no such property: n for class: Test)

 

Clearly, neither n nor m is a field of class Test, nor are they present in the binding. However, m is still accessible to be an argument to the add method.

 

I can easily tell the user not to specify def in front of variables, only in front of methods, but I cannot fully explain why.

 

Where is the variable stored? In the main method? or just inline replaced during parsing? Hope you can help me with an answer.

 

Regards,

/Niels


This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.

 


This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.


Re: What happens to 'def' attributes in scripts?

by Jochen Theodorou :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Niels B Nielsen schrieb:
> Great thanks,
>
> Just as I expected. That leads me to the following questions..

Edward was nearly right,

n=1
m=2
def add(another) {
   int x = n+another
   println "x is ${x}"
}
add(m)

becomes:

class Foo extends Script {
   def run() {
     n=1
     m=1
     add(m)
   }
   def add(another){
     int x = n+another
     println "x is $x"
   }
}


> 1) Why does methods have to be def'ed.
 > 2) How can I specify the type of my attribute?

def stands for a definition of either a method or variable. Types are
usually optional, but then Object is used. So your add methods takes an
Object parameter and returns an Object. Methods don't have to be def'ed,
you can also write for example:

long add(int a, long b){a+b}

> Some of the users have programming experience and
> a) either want def func() and def var to be consistent
> b) by mistake write int n=1 very often.

for grammar reasonssome kind of "introduction part" is needed. For
example this

static main(String[] args) {...}

does neither use def nor a type, still you define a method, because that
static is good enough for the compiler to recognize the definition. This
main method here will of course again return Object, for void, you have
to write void.

Does this answer your questions?

bye blackdrag

--
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: What happens to 'def' attributes in scripts?

by Martin C. Martin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



Jochen Theodorou wrote:

> Niels B Nielsen schrieb:
>> Great thanks,
>>
>> Just as I expected. That leads me to the following questions..
>
>> 1) Why does methods have to be def'ed.
>  > 2) How can I specify the type of my attribute?
>
> def stands for a definition of either a method or variable. Types are
> usually optional, but then Object is used.  So your add methods takes an
> Object parameter and returns an Object. Methods don't have to be def'ed,
> you can also write for example:
>
> long add(int a, long b){a+b}

To clarify a bit: "def" is only needed when the syntax is ambiguous.
Usually, that's when you don't specify a return type or any modifier.
So this is also valid (I think):

static add(a, b) { a+b }

I think your question is, "when I don't specify a type, why do I need
def for methods but not for script variables?"

An ambiguity comes with an expression like:

x = 23

If x hasn't been declared before, should this be the definition of a
local variable, or a setting a property on "this"?  Inside (explicit)
methods, we treat it as setting a property, and if you want the
definition, you need to add "def" (or return type, or modifier key word).

Scripts are different, the fact that you're defining a method (run) is a
kind of implementation detail.  We pretend its a kind of global scope,
so there's no property, so that a variable declaration is the best
interpretation.

Hope that helps,
Martin


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email