groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MG <mg...@arscreat.com>
Subject Re: Omitting parentheses on .size() ?
Date Sat, 01 Aug 2020 21:39:33 GMT
On 01/08/2020 20:55, Jochen Theodorou wrote:
> On 01.08.20 19:41, MG wrote:
>>
>> This has of course been discussed many times before - maybe a "size"
>> operator:
>>
>> #list // list.size()
>> #map // map.size()
>> #string // string.length
>
> a size() method is the way of consistency we did go for. It exists on
> collection, string, array and map. We added it to string and array so
> you do not to have to remember using length or length()

I know, and that was and is a good idea imho. Just still feels 
non-Groovy to always have to write "${p.size()}" instead of just 
"$p.size"* :-)

("$#p" would need to be supported for this to be helpful in this case, 
evidently)

>
>> which people could override (e.g. sizeOf() method) and which unifies all
>> kinds of "how many elements do I hold / how big am I" concepts (if its
>> not a collection holding items) ?-)
>
> why sizeOf() if there is already size()?

Because it would be the method corresponding to the operator, and while 
I would make sizeOf() default to size() on maps, collections, etc, it 
would allow for sizeOf to be different from size() (the developer would 
have to check if this violates least surprise).

I  just threw the idea out there, but the "#"-operator could e.g. be 
used by someone to return how many children a abstract node or a 
concrete person has, if that made sense in his framework.

Cheers,
mg

PS: If there is further discussion on this, we should probably move it 
to dev...










Mime
View raw message