Groovy

Bruno Medeiros brunodomedeiros+spam at com.gmail
Sun Dec 31 12:34:58 PST 2006


Thomas Kuehne wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Groovy(http://groovy.codehaus.org/) is a dynamic language implemented on
> the Java VM with strong Java integration. As Groovy's roots and
> aims partially overlap with D's, some solutions might be interesting.
> 
> DISCLAIMER: I'm not a Groovy expert.
> 
> 1) ranges
> 
> Groovy:
> #
> # assert (x in 2 .. 5);
> #
> # switch(y) {
> #    case 1: break;
> #    case 1900 .. 2006: break;
> # }
> #
> 
> current D:
> #
> # assert((2 <= x) && (x <= 5));
> #
> # switch(y) {
> #    case 1: break;
> #    default:
> #       if((1900 <= y) && (y <= 2006)){
> #          break;
> #       }
> # }
> 
> Especially the ranges support for switch/case is very useful.
> 
> 
> 2) literals for associative arrays
> 
> Groovy:
> #
> # def map = ['a': 1, 'b': 2, 'c': 3]
> #
> 

'def' for auto type deduction. Nice. Again, I like it more than 'auto'.
As for range types, and map literals, I wouldn't mind them in the 
language, but I've never felt much need for them , but that's likely 
just me. (in the code I've seen, I don't recall a place where it could 
be used, at least in non-temporary code)


> current D:
> #
> # int[char] map;
> # map['a'] = 1;
> # map['b'] = 2;
> # map['c'] = 3;
> #
> 
> Other useful features of Groovy depend too much on the VM
> (e.g. dynamic extending of classes) for integration into D.
> 
> Thomas
> 
> 
> -----BEGIN PGP SIGNATURE-----
> 
> iD8DBQFFmACaLK5blCcjpWoRAgKqAJ9BZIaQcCAux4EJn8ZHD4MyJdpn8QCfTb0N
> iq1UGHbq25cNJCN8ZPv+RnY=
> =IPRp
> -----END PGP SIGNATURE-----


I just took a quick look under their site, but I've stumbled on 
something very interesting:

---- quote from: http://groovy.codehaus.org/Quick+Start ----
If no parameter(s) is(are) specified before -> symbol then a default 
named parameter, called 'it' can be used. e.g.

def closure = { println "hello " + it }
closure.call("world!")
----

I'm thinking if something like that can be used to ease idioms like:
   intArray.apply( (int e) { e++;} );
where the "(int e)" is mostly redundant. Such that it would be nice if 
the language could support a syntax like this:
   intArray.apply( { it++;} );

It could work in D in two ways, both with different semantics and 
advantages.
One is like what I bet is the Groovy way (didn't look enough to 
confirm), where 'it' is a generic parameter (erased to Object in 
runtime, and primitives are boxed). But that doesn't fit well with D.
Another way is to deduce the full delegate type from the context 
(somewhat like string literals), so that in the example above the 
delegate literal gets deduced to a "(int it) { it++;}" type. But that 
has some problems, like you can't do stuff like
   dg = { it++; };
because then you cannot deduce dg's type, so this behavior also seems 
sub-optimal.
Anyways my point is this is an issue worthy of thinking about.

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list