Create new java.util.Set in groovy

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

Create new java.util.Set in groovy

Tomas Hubalek-3
Hello,

is there nicer way how to create in Groovy new Set instance? Currently
I'm using

Set set = new HashSet( ["A","B"] )

but maybe there is something like

Set set = ["A","B"]

or

Set set = ["A","B"] as Set

but I cannot found it...

Thanks
Tom
Reply | Threaded
Open this post in threaded view
|

Re: Create new java.util.Set in groovy

Alan Green-2
Tomas Hubalek wrote:
> Hello,
> is there nicer way how to create in Groovy new Set instance? Currently
> I'm using
>
> Set set = new HashSet( ["A","B"] )

This is as compact a syntax as Groovy currently allows:

[['a','b']] as HashSet

Still not very nice, though. You might try submitting a JIRA request for
'as Set'.

Alan.
Reply | Threaded
Open this post in threaded view
|

Re: Create new java.util.Set in groovy

Alan Green-2
Alan Green wrote:
> This is as compact a syntax as Groovy currently allows:
>
> [['a','b']] as HashSet
>

Guillaume pointed out that perhaps I should explain why this strange
expression works.

When Groovy sees "x as Y", it tries a number of strategies to convert
the object x into an instance of class Y. One of the last of these is to
check whether x is a list, and if Y has a constructor that matches the
number and type of the elements of x, it calls the constructor.

(This is the mechanism that allows "[200, 200] as java.awt.Dimension" to
work.)

So, when Groovy sees "[['a', 'b']] as HashSet", it calls the HashSet
constructor that has a single java.util.Collection parameter. "['a',
'b'] as HashSet" doesn't work because HashSet doesn't have a constructor
that takes two string parameters, and none of the other "as" strategies
work either.

It would be *much* nicer if Groovy allowed "['a', 'b'] as Set". Even
"['a', 'b'] as HashSet" would be OK.

Cheers,

Alan.
Reply | Threaded
Open this post in threaded view
|

Re: Re: Create new java.util.Set in groovy

Guillaume Laforge
Administrator
On 14/12/05, Alan Green <[hidden email]> wrote:

> Guillaume pointed out that perhaps I should explain why this strange
> expression works.
>
> When Groovy sees "x as Y", it tries a number of strategies to convert
> the object x into an instance of class Y. One of the last of these is to
> check whether x is a list, and if Y has a constructor that matches the
> number and type of the elements of x, it calls the constructor.
>
> (This is the mechanism that allows "[200, 200] as java.awt.Dimension" to
> work.)

Ah right, this is all clear now! Thanks!

> So, when Groovy sees "[['a', 'b']] as HashSet", it calls the HashSet
> constructor that has a single java.util.Collection parameter. "['a',
> 'b'] as HashSet" doesn't work because HashSet doesn't have a constructor
> that takes two string parameters, and none of the other "as" strategies
> work either.
>
> It would be *much* nicer if Groovy allowed "['a', 'b'] as Set". Even
> "['a', 'b'] as HashSet" would be OK.

Yeah, it'd be much nicer, and I think we should definitely support
that notation (the latter).
Could you please file a JIRA issue about it?

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|

Re: Re: Create new java.util.Set in groovy

Tomas Hubalek-3
In reply to this post by Alan Green-2
Alan,

thanks for nice introduction to next step of groovy wizardry ;-)

Regarding syntax I vote for support for

['a','b'] as Set // it could use some default Set implementation, say
HashSet

or at least

['a','b'] as HashSet

which is make sense.

But I don't know how to solve problem if (hypothetically) HashSet had
two string parameters in constructor and also collection as constructor.

Thanks a lot
Tom

Alan Green napsal(a):

> Alan Green wrote:
>
>> This is as compact a syntax as Groovy currently allows:
>>
>> [['a','b']] as HashSet
>>
>
> Guillaume pointed out that perhaps I should explain why this strange
> expression works.
>
> When Groovy sees "x as Y", it tries a number of strategies to convert
> the object x into an instance of class Y. One of the last of these is
> to check whether x is a list, and if Y has a constructor that matches
> the number and type of the elements of x, it calls the constructor.
>
> (This is the mechanism that allows "[200, 200] as java.awt.Dimension"
> to work.)
>
> So, when Groovy sees "[['a', 'b']] as HashSet", it calls the HashSet
> constructor that has a single java.util.Collection parameter. "['a',
> 'b'] as HashSet" doesn't work because HashSet doesn't have a
> constructor that takes two string parameters, and none of the other
> "as" strategies work either.
>
> It would be *much* nicer if Groovy allowed "['a', 'b'] as Set". Even
> "['a', 'b'] as HashSet" would be OK.
>
> Cheers,
>
> Alan.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Re: Create new java.util.Set in groovy

graemer
+1 for this as Grails uses Sets a lot to define ORM relationships

Graeme

On 14/12/05, Tomas Hubalek <[hidden email]> wrote:

> Alan,
>
> thanks for nice introduction to next step of groovy wizardry ;-)
>
> Regarding syntax I vote for support for
>
> ['a','b'] as Set // it could use some default Set implementation, say
> HashSet
>
> or at least
>
> ['a','b'] as HashSet
>
> which is make sense.
>
> But I don't know how to solve problem if (hypothetically) HashSet had
> two string parameters in constructor and also collection as constructor.
>
> Thanks a lot
> Tom
>
> Alan Green napsal(a):
>
> > Alan Green wrote:
> >
> >> This is as compact a syntax as Groovy currently allows:
> >>
> >> [['a','b']] as HashSet
> >>
> >
> > Guillaume pointed out that perhaps I should explain why this strange
> > expression works.
> >
> > When Groovy sees "x as Y", it tries a number of strategies to convert
> > the object x into an instance of class Y. One of the last of these is
> > to check whether x is a list, and if Y has a constructor that matches
> > the number and type of the elements of x, it calls the constructor.
> >
> > (This is the mechanism that allows "[200, 200] as java.awt.Dimension"
> > to work.)
> >
> > So, when Groovy sees "[['a', 'b']] as HashSet", it calls the HashSet
> > constructor that has a single java.util.Collection parameter. "['a',
> > 'b'] as HashSet" doesn't work because HashSet doesn't have a
> > constructor that takes two string parameters, and none of the other
> > "as" strategies work either.
> >
> > It would be *much* nicer if Groovy allowed "['a', 'b'] as Set". Even
> > "['a', 'b'] as HashSet" would be OK.
> >
> > Cheers,
> >
> > Alan.
> >
> >
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Re: Create new java.util.Set in groovy

Jochen Theodorou
In reply to this post by Guillaume Laforge
Guillaume Laforge schrieb:
[...]
>>It would be *much* nicer if Groovy allowed "['a', 'b'] as Set". Even
>>"['a', 'b'] as HashSet" would be OK.
>
> Yeah, it'd be much nicer, and I think we should definitely support
> that notation (the latter).

we can do this:

['a', 'b'].asSet()

that's a new Method for DefaultGroovyMethod and would avoid the problems
of the constructor. Btw: I thought we wanted to support as operator. If
we do so, the operator can be overloaded...

example:

['a', 'b'] as Set

becomes

['a', 'b'].as(Set)

This means a method named "as" taking a class.

bye blackdrag
Reply | Threaded
Open this post in threaded view
|

Re: Re: Create new java.util.Set in groovy

Marc Palmer Local

On 14 Dec 2005, at 11:30, Jochen Theodorou wrote:

> we can do this:
>
> ['a', 'b'].asSet()
>
> that's a new Method for DefaultGroovyMethod and would avoid the  
> problems of the constructor. Btw: I thought we wanted to support as  
> operator. If we do so, the operator can be overloaded...
>
> example:
>
> ['a', 'b'] as Set
>
> becomes
>
> ['a', 'b'].as(Set)
>
> This means a method named "as" taking a class.


Argh! This seems really obscure. I know I am a relative Groovy newbie  
but what I'm observing here is a lot of talk of what the Syntax  
should be based on how it would be implemented internally with Java.

This seems like completely the wrong approach to me - as the  
mechanism you choose may not make sense to people who know Groovy but  
not Java - which if Groovy is actually successful will hopefully be a  
lot of people.

[x, y] as Set is the only mechanism that I've seen so far that looks  
easy to understand and consistent. However, if Groovy does not  
already support "x as Map" and only "x as HashMap" then it should of  
course be "[x, y] as HashSet"

Already the rules explained for the way "as" works are probably going  
to be too confusing for most Groovy-only people, as the description  
relies quite a lot on Java imo.

As I said before, for people to really not be confused about Groovy  
and understand it as a language in its own right, I'm pretty  
convinced that describing things in terms of Java is bad news.


Cheers
~ ~ ~
Marc Palmer ([hidden email])
Consultant/Analyst
AnyWare Ltd.
http://www.anyware.co.uk/

Reply | Threaded
Open this post in threaded view
|

Re: Re: Create new java.util.Set in groovy

Tomas Hubalek-3
In reply to this post by Jochen Theodorou
Jochen Theodorou napsal(a):

> that's a new Method for DefaultGroovyMethod and would avoid the
> problems of the constructor. Btw: I thought we wanted to support as
> operator. If we do so, the operator can be overloaded...
>
> example:
>
> ['a', 'b'] as Set
>
> becomes
>
> ['a', 'b'].as(Set)
>
> This means a method named "as" taking a class.

This very nice and clean solution. I like it...

Tom
Reply | Threaded
Open this post in threaded view
|

Re: Re: Create new java.util.Set in groovy

Russel Winder
In reply to this post by Jochen Theodorou
On Wed, 2005-12-14 at 12:30 +0100, Jochen Theodorou wrote:

> ['a', 'b'].asSet()

I'm not keep on this.  Two reasons:

1.  The type is encoded in the name of the method so it means a new
method for every type.

2.  There is already a 'X as Y' mechanism.
 
> ['a', 'b'] as Set
>
> becomes
>
> ['a', 'b'].as(Set)
>
> This means a method named "as" taking a class.

This actually seems like a proper solution to the whole problem unless I
am missing something.

[ 'a' , 'b' ] as Char[]

then becomes [ 'a' , 'b' ].as ( Char[] )

and the whole 'X as Y' syntax can be removed.  This new proposal is
consistent in that it is a construction method applied to a list:  the
'X as Y' always did seem a little incongruous.

Of course in all these discussions Set is an interface and not a Class
so either we have to choose a default representation or we have to
require a Class not an interface.

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
12