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
|

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

Marc Palmer Local

On 14 Dec 2005, at 11:46, Russel Winder wrote:

>> ['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.

If x.as(Class) is more generic and consistent with the rest of Groovy  
then I'm with you on that.

As long as there is only one way to do coercions :)

~ ~ ~
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

Martin C. Martin
In reply to this post by Marc Palmer Local


Marc Palmer wrote:

>
> 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.

I don't really see your point.  First of all, Groovy uses all of the
Java library, so anyone who uses Groovy will have to be able to read the
JDK documentation and understand how Groovy gives an alternate way to
access it, e.g. x.getFoo() can be accessed as x.foo.  Also, if they ever
want to use an external Java library in their code, they'll be in the
same situation.

But that actually doesn't apply to the x.as(Y) example.  That's pure
Groovy, has nothing to do with Java.  The idea is that Groovy could
extend the Java type by adding a function, as(), which takes a Class and
is responsible for converting x to class Y.  You can convert it however
you want, independent of how Java does it.

- Martin
Reply | Threaded
Open this post in threaded view
|

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

Martin C. Martin
In reply to this post by Russel Winder


Russel Winder wrote:

> 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.

I don't think Jochen is talking about replacing the existing mechanism;
he's talking about how you could add your own semantics to the 'X as Y'
statement.  Just as by defining X.plus(Y) sets the semantics for x + y.

>>['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.
>
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 Marc Palmer Local
Marc Palmer schrieb:
[...]

>> ['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.

nono, many operators are overloadable in Groovy. there is +,-,*,<< for
example. So "as" woudl just be another operator. I just want to avoid
the problems we have with this syntax:

new Foo(a:b)

this creates a new Foo object and sets the property a to b. *But* only
if the constructor of Foo is not taking a Map, than this constructor is
used instead. And now imagine you have both, a constructor taking a Map
and a constructor taking no arguments, but having a property named a.
This contruction is too ambigous for me. So there are two solutions,
invalidate one of these two possibilities, or make it customizable.
Both of these solutions are speaking against "[] as Set" as compiler
controled construct, if we want to keep the current behavior.

> 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.

all Operators can be defined in Groovy.

class Foo {
   @Property i=0;

   def plus(Foo f) {
     def ret = new Foo()
     ret.i=f.i+this.i
     return ret
   }
}

def f1 = new Foo(i:1)
def f2 = new Foo(i:2)
def f3 = f1+f2

assert f3.i==3

It would be the same for "as"

> [x, y] as Set is the only mechanism that I've seen so far that looks  
> easy to understand and consistent.

Well, you don't have to know the internals ;) At last as long as nobody
adds methods to List.

[...]
> 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.

You mean saying that "['a'] as Foo" relies on Foo taking one String
argument is relying on Java? Foo can be written in Groovy.

> 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.

ah maybe you misunderstood in that way, that "[] as Set" will be
implemented by List#as(Class), but you can still write "[] as Set". aS
for nearly all operators you have two ways of writing them. The short
Form with the Operatorsymbol, and the longer form as methodcall. Of
course "as" is in both ways nearly of the same length, so there is no
real shorter form ;)

By 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 12:56, Jochen Theodorou wrote:

> Marc Palmer schrieb:
> [...]
>>> ['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.
>
> nono, many operators are overloadable in Groovy. there is +,-,*,<<  
> for example. So "as" woudl just be another operator. I just want to  
> avoid the problems we have with this syntax:
>

Thanks for the explanations Jochen.

I'm really looking forward to the definitive language spec instead of  
grabbing what you can from bits of web docs here and there.

Cheers


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

Reply | Threaded
Open this post in threaded view
|

Re: Create new java.util.Set in groovy

Alan Green-2
In reply to this post by Guillaume Laforge
Guillaume Laforge wrote:
> 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?

Turns out that Dierk is a month and a half ahead of us and has already
filed an issue! I added some comments:

http://jira.codehaus.org/browse/GROOVY-1115

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:
> Turns out that Dierk is a month and a half ahead of us and has already
> filed an issue! I added some comments:
>
> http://jira.codehaus.org/browse/GROOVY-1115

It was already there then... :-)
Thanks a lot Alan.

--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy
12