You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "David M. Karr" <da...@gmail.com> on 2016/01/13 19:38:18 UTC

Unexpected use of "collection.field.first()"

Today I noticed an unexpected way the "first()" function behaves. I'd 
appreciate some clarification.

With the following data:

def data = [
   name : 'Ivan', age: 35,
   childs : [
     [name: 'Judith', age:8], [name: 'Adriana', age:5]
     ]
]

The following two statements produce the same result:

println data.childs.first().name
println data.childs.name.first()

Being "Judith".

The first one is obvious to me.  The second one is unexpected. What's 
going on here exactly?

Re: Unexpected use of "collection.field.first()"

Posted by "David M. Karr" <da...@gmail.com>.
On 01/13/2016 11:59 AM, Jennifer Strater wrote:
> I found two different ways to answer this.
>
> 1.) 
> http://stackoverflow.com/questions/32723147/groovy-implicit-spread-dot-operator
> This question from stackoverflow, which references the Groovy 
> documentation for GPath expressions.
>
> 2.) 
> http://naleid.com/blog/2008/12/24/groovy-spread-operator-optional-for-properties-plus-a-peek-into-the-sausage-factory/
> This wonderful blog post that dives into the details.
>
> Is this what you were looking for?

Uh, yeah, and now I see that I've already gone down this path before, as 
I was one of the commenters on that posting. :)

>
> On Wed, Jan 13, 2016 at 1:09 PM, David M. Karr 
> <davidmichaelkarr@gmail.com <ma...@gmail.com>> wrote:
>
>
>
>     On 01/13/2016 10:50 AM, Jennifer Strater wrote:
>>     Hi David,
>>
>>     I assume the confusion is related to the implicit spread-dot? If
>>     you look at the result of data.childs.name
>>     <http://data.childs.name> you will find [Judith,Adriana].  It
>>     then takes the first element of that list.
>
>     Oh, right.  So when does Groovy do the spread-dot operator
>     "implicitly"?
>
>>
>>     -Jenn
>>
>>     On Wed, Jan 13, 2016 at 12:38 PM, David M. Karr
>>     <davidmichaelkarr@gmail.com <ma...@gmail.com>>
>>     wrote:
>>
>>         Today I noticed an unexpected way the "first()" function
>>         behaves. I'd appreciate some clarification.
>>
>>         With the following data:
>>
>>         def data = [
>>           name : 'Ivan', age: 35,
>>           childs : [
>>             [name: 'Judith', age:8], [name: 'Adriana', age:5]
>>             ]
>>         ]
>>
>>         The following two statements produce the same result:
>>
>>         println data.childs.first().name
>>         println data.childs.name.first()
>>
>>         Being "Judith".
>>
>>         The first one is obvious to me.  The second one is
>>         unexpected. What's going on here exactly?
>>
>>
>
>


Re: Unexpected use of "collection.field.first()"

Posted by Jennifer Strater <je...@gmail.com>.
I found two different ways to answer this.

1.)
http://stackoverflow.com/questions/32723147/groovy-implicit-spread-dot-operator
This question from stackoverflow, which references the Groovy documentation
for GPath expressions.

2.)
http://naleid.com/blog/2008/12/24/groovy-spread-operator-optional-for-properties-plus-a-peek-into-the-sausage-factory/
This wonderful blog post that dives into the details.

Is this what you were looking for?

On Wed, Jan 13, 2016 at 1:09 PM, David M. Karr <da...@gmail.com>
wrote:

>
>
> On 01/13/2016 10:50 AM, Jennifer Strater wrote:
>
> Hi David,
>
> I assume the confusion is related to the implicit spread-dot? If you look
> at the result of data.childs.name you will find [Judith,Adriana].  It
> then takes the first element of that list.
>
>
> Oh, right.  So when does Groovy do the spread-dot operator "implicitly"?
>
>
> -Jenn
>
> On Wed, Jan 13, 2016 at 12:38 PM, David M. Karr <
> davidmichaelkarr@gmail.com> wrote:
>
>> Today I noticed an unexpected way the "first()" function behaves. I'd
>> appreciate some clarification.
>>
>> With the following data:
>>
>> def data = [
>>   name : 'Ivan', age: 35,
>>   childs : [
>>     [name: 'Judith', age:8], [name: 'Adriana', age:5]
>>     ]
>> ]
>>
>> The following two statements produce the same result:
>>
>> println data.childs.first().name
>> println data.childs.name.first()
>>
>> Being "Judith".
>>
>> The first one is obvious to me.  The second one is unexpected. What's
>> going on here exactly?
>>
>
>
>

Re: Unexpected use of "collection.field.first()"

Posted by "David M. Karr" <da...@gmail.com>.

On 01/13/2016 10:50 AM, Jennifer Strater wrote:
> Hi David,
>
> I assume the confusion is related to the implicit spread-dot? If you 
> look at the result of data.childs.name <http://data.childs.name> you 
> will find [Judith,Adriana].  It then takes the first element of that list.

Oh, right.  So when does Groovy do the spread-dot operator "implicitly"?

>
> -Jenn
>
> On Wed, Jan 13, 2016 at 12:38 PM, David M. Karr 
> <davidmichaelkarr@gmail.com <ma...@gmail.com>> wrote:
>
>     Today I noticed an unexpected way the "first()" function behaves.
>     I'd appreciate some clarification.
>
>     With the following data:
>
>     def data = [
>       name : 'Ivan', age: 35,
>       childs : [
>         [name: 'Judith', age:8], [name: 'Adriana', age:5]
>         ]
>     ]
>
>     The following two statements produce the same result:
>
>     println data.childs.first().name
>     println data.childs.name.first()
>
>     Being "Judith".
>
>     The first one is obvious to me.  The second one is unexpected.
>     What's going on here exactly?
>
>


Re: Unexpected use of "collection.field.first()"

Posted by Jennifer Strater <je...@gmail.com>.
Hi David,

I assume the confusion is related to the implicit spread-dot? If you look
at the result of data.childs.name you will find [Judith,Adriana].  It then
takes the first element of that list.

-Jenn

On Wed, Jan 13, 2016 at 12:38 PM, David M. Karr <da...@gmail.com>
wrote:

> Today I noticed an unexpected way the "first()" function behaves. I'd
> appreciate some clarification.
>
> With the following data:
>
> def data = [
>   name : 'Ivan', age: 35,
>   childs : [
>     [name: 'Judith', age:8], [name: 'Adriana', age:5]
>     ]
> ]
>
> The following two statements produce the same result:
>
> println data.childs.first().name
> println data.childs.name.first()
>
> Being "Judith".
>
> The first one is obvious to me.  The second one is unexpected. What's
> going on here exactly?
>