Quantcast

def or not def ?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

def or not def ?

Ista Pouss
Hi,

I don't understand the def keyword. if I put it, it's work ok.

But sometime  I forget it. In this case, sometimes my script works,
sometimes it doesn't work, I don't understand why.

Thanks.

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: def or not def ?

Matthew.Lachman
Hi Ista,

def is a Groovy keyword that is essentially a replacement for "Object".
If you are creating a variable for instance, you need to give it a type
or use the def (Object) keyword:

    def foo = "bar"

When you are creating a method, you don't need to give the parameters
types.  So you can create a method signature like this and it will work:

    def myMethod(a, b) {
        //do something interesting
    }

This post on Stack Overflow has a good amount of information about the
"def" keyword:
http://stackoverflow.com/questions/184002/groovy-whats-the-purpose-of-de
f-in-def-x-0

It also links to this page:
http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22

Matt

-----Original Message-----
From: Ista Pouss [mailto:[hidden email]]
Sent: Friday, November 20, 2009 2:50 PM
To: [hidden email]
Subject: [groovy-user] def or not def ?

Hi,

I don't understand the def keyword. if I put it, it's work ok.

But sometime  I forget it. In this case, sometimes my script works,
sometimes it doesn't work, I don't understand why.

Thanks.

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

    http://xircles.codehaus.org/manage_email




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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: def or not def ?

paulk_asert
In reply to this post by Ista Pouss

In classes and scripts, def acts as a placeholder for the type.
It is essentially like having Object there. You don't need def
if you have some other type or modifier.

In scripts, you can leave off the def altogether in which case
the variable will come from the binding.

Paul.

Ista Pouss wrote:

> Hi,
>
> I don't understand the def keyword. if I put it, it's work ok.
>
> But sometime  I forget it. In this case, sometimes my script works,
> sometimes it doesn't work, I don't understand why.
>
> Thanks.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: def or not def ?

Martin C. Martin-2
This is a FAQ, we should probably have a wiki page on it.

 > In classes and scripts, def acts as a placeholder for the type.
 > It is essentially like having Object there.

Actually, this is a common misconception.  "def" is required when there
is nothing else to signal that this is a declaration.  So if you have a
modifier, you don't need a type or "def":

static foo

final foo

Also, you can put in a def when you don't really need it, so you could
write:

def String foo

or even

String def foo

and the compiler doesn't complain that you're declaring foo to be both
String and Object.

Paul King wrote:

>
> You don't need def
> if you have some other type or modifier.
>
> In scripts, you can leave off the def altogether in which case
> the variable will come from the binding.
>
> Paul.
>
> Ista Pouss wrote:
>> Hi,
>>
>> I don't understand the def keyword. if I put it, it's work ok.
>>
>> But sometime  I forget it. In this case, sometimes my script works,
>> sometimes it doesn't work, I don't understand why.
>>
>> Thanks.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: def or not def ?

Ista Pouss
2009/11/21 Martin C. Martin <[hidden email]>:
> This is a FAQ, we should probably have a wiki page on it.
>

Yes on the wiki at
http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22
please excuse me.

But this page begin with this curious note : "This page may not follow
the explanations from the JLS." A fork in the explanations ?

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: def or not def ?

paulk_asert
In reply to this post by Martin C. Martin-2

Yep, I think we are saying the same thing. ;-)

Paul.

Martin C. Martin wrote:

> This is a FAQ, we should probably have a wiki page on it.
>
>  > In classes and scripts, def acts as a placeholder for the type.
>  > It is essentially like having Object there.
>
> Actually, this is a common misconception.  "def" is required when there
> is nothing else to signal that this is a declaration.  So if you have a
> modifier, you don't need a type or "def":
>
> static foo
>
> final foo
>
> Also, you can put in a def when you don't really need it, so you could
> write:
>
> def String foo
>
> or even
>
> String def foo
>
> and the compiler doesn't complain that you're declaring foo to be both
> String and Object.
>
> Paul King wrote:
>>
>> You don't need def
>> if you have some other type or modifier.
>>
>> In scripts, you can leave off the def altogether in which case
>> the variable will come from the binding.
>>
>> Paul.
>>
>> Ista Pouss wrote:
>>> Hi,
>>>
>>> I don't understand the def keyword. if I put it, it's work ok.
>>>
>>> But sometime  I forget it. In this case, sometimes my script works,
>>> sometimes it doesn't work, I don't understand why.
>>>
>>> Thanks.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>     http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


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

    http://xircles.codehaus.org/manage_email


Loading...