Julien Ponge | 4 Sep 21:33
Gravatar

[groovy-user] Support for unless / if expressions

Hi everyone,

I have filled a JIRA issue
(http://jira.codehaus.org/browse/GROOVY-2892) a few weeks backs with
the idea of adding support for unless / if expressions to Groovy,
like:

doSomething() unless (x > 10)
(...)
triggerSomethingNasty() if (x < 10)

Jochen suggested discussing this with the Groovy users community.

What do you think?

--

-- 
http://izpack.org/
http://jpz-log.info/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Luke Daley | 5 Sep 01:27

Re: [groovy-user] Support for unless / if expressions


On 05/09/2008, at 5:34 AM, Julien Ponge wrote:

> I have filled a JIRA issue
> (http://jira.codehaus.org/browse/GROOVY-2892) a few weeks backs with
> the idea of adding support for unless / if expressions to Groovy,
> like:
>
>
> Jochen suggested discussing this with the Groovy users community.
>
> What do you think?

+1000 for unless, except I think we should stick with pre statement  
rather post statement. To be clear…

unless (x > 10) doSomething()

if (x < 10) triggerSomethingNasty()

I actually prefer the ruby/perl style of having the condition  
afterwards for single statement expressions, but since Groovy already  
supports have the condition before the statement I would advocate  
sticking with this.

--

LD.

---------------------------------------------------------------------
(Continue reading)

Stand Trooper | 5 Sep 02:38

Re: [groovy-user] Support for unless / if expressions

As a post-condition I'd have to pass for sure on this one.

As a pre-condition I don't see the potential for it.  I would suppose that if I used Ruby or Python that would be different.  I would need more of a reason to push for an additional reason to bloviate the language even more when I would look to an if...elseif...else or another conditional to push my objective.

As for additions to the language, I don't have issues with that, but I've read the ticket and except in the case that Paul King put forth with the do...unless I don't see this as anything useful.  In all the years of programming I can tell you I've never once used a do...while outside of classrooms.

Are there other practical examples of this usage that might sway my opinion?  Otherwise, I just don't see it.  If it's to be added as syntactic sugar in place of an else statement, then I suppose that would be acceptable, but I don't see it as anything spectacular that an else couldn't do.

Did I miss something?

timo

On Thu, Sep 4, 2008 at 6:27 PM, Luke Daley <ld <at> ldaley.com> wrote:

On 05/09/2008, at 5:34 AM, Julien Ponge wrote:

I have filled a JIRA issue
(http://jira.codehaus.org/browse/GROOVY-2892) a few weeks backs with
the idea of adding support for unless / if expressions to Groovy,
like:


Jochen suggested discussing this with the Groovy users community.

What do you think?

+1000 for unless, except I think we should stick with pre statement rather post statement. To be clear…

unless (x > 10) doSomething()

if (x < 10) triggerSomethingNasty()

I actually prefer the ruby/perl style of having the condition afterwards for single statement expressions, but since Groovy already supports have the condition before the statement I would advocate sticking with this.

--

LD.
Luke Daley | 5 Sep 02:59

Re: [groovy-user] Support for unless / if expressions


On 05/09/2008, at 10:38 AM, Stand Trooper wrote:

> As a post-condition I'd have to pass for sure on this one.
>
> As a pre-condition I don't see the potential for it.  I would  
> suppose that if I used Ruby or Python that would be different.  I  
> would need more of a reason to push for an additional reason to  
> bloviate the language even more when I would look to an  
> if...elseif...else or another conditional to push my objective.

This isn't completely about post vs. pre condition. More about the  
syntax of supporting the condition being place post statement.

> Are there other practical examples of this usage that might sway my  
> opinion?  Otherwise, I just don't see it.  If it's to be added as  
> syntactic sugar in place of an else statement, then I suppose that  
> would be acceptable, but I don't see it as anything spectacular  
> that an else couldn't do.

unless (thing) object.doStuff()

is better than

`if (!thing) object.doStuff()` or `if (thing == false) object.doStuff()`

in terms of readability. Particularly when using ! as that is very  
easy to miss. It's a simple thing, but it can surprisingly make code  
a _lot_ more readable.

--

LD.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Stand Trooper | 5 Sep 05:08

Re: [groovy-user] Support for unless / if expressions

That clears it up sort of... so really unless is an evaluation of a negative then?

you would think that after 9+ years of working for internet companies that search engines would be where I would go to look up answers first.  In any case, I will (in my ignorance of unless) continue:

the unless would evaluate the groovy false where the if evaluates the groovy true and currently necessitates the bang or equivalent (as pointed out)

I can agree with this logic and as such reverse my opinion based on this presentation.

I support the addition of unless to the language as well.

timo

On Thu, Sep 4, 2008 at 7:59 PM, Luke Daley <ld-Hc2V4H83aCrQT0dZR+AlfA@public.gmane.org> wrote:

unless (thing) object.doStuff()

is better than

`if (!thing) object.doStuff()` or `if (thing == false) object.doStuff()`

in terms of readability. Particularly when using ! as that is very easy to miss. It's a simple thing, but it can surprisingly make code a _lot_ more readable.


--

LD.

Martin C. Martin | 5 Sep 03:20

Re: [groovy-user] Support for unless / if expressions

This is something that seemed squirelly to me until I started using Ruby 
3 months ago.  I was surprised how quickly I went from disliking it to 
liking it.  The common pattern is that it's used for error checking or 
some other corner case.  When used that way, it's actually more 
readable, because the "then part" is obviously an error condition, and 
the eye can usually determine that more quickly than when looking at the 
condition.  For example:

def myFunMethod(int foo) {
   throw new RuntimeException("Arg must be positive") if (foo < 0)
   ...

The "then part" is usually something obviously odd, such as throwing an 
Exception, returning when the result hasn't been computed, or assigning 
null when everything is going well.  When I see this, I think "probably 
error-handling", check for an "if" or "unless," and skip to the next line.

Of course, it's possible to use this to write confusing code.  Any 
language feature can be abused to make things more confusing.  But infix 
"if" lets you recognize and skip corner cases more quickly when you're 
interested in the typical case (which is most of the time).  And it 
doesn't slow you down when you're interested in the corner case.

Stand, have you had to read code in Ruby, Python or any other language 
that has this?  Code that uses the construct?

There's less of an argument to "unless."  Dealing with negatives is 
confusing.  I actually programmed Lisp for two years before the Ruby 
gig, and never really got used to "unless."  If we introduce it as 
infix, I think we should add it as prefix as well.  But I don't think we 
should do either.

Best,
Martin

Stand Trooper wrote:
> As a post-condition I'd have to pass for sure on this one.
> 
> As a pre-condition I don't see the potential for it.  I would suppose 
> that if I used Ruby or Python that would be different.  I would need 
> more of a reason to push for an additional reason to bloviate the 
> language even more when I would look to an if...elseif...else or another 
> conditional to push my objective.
> 
> As for additions to the language, I don't have issues with that, but 
> I've read the ticket and except in the case that Paul King put forth 
> with the do...unless I don't see this as anything useful.  In all the 
> years of programming I can tell you I've never once used a do...while 
> outside of classrooms.
> 
> Are there other practical examples of this usage that might sway my 
> opinion?  Otherwise, I just don't see it.  If it's to be added as 
> syntactic sugar in place of an else statement, then I suppose that would 
> be acceptable, but I don't see it as anything spectacular that an else 
> couldn't do.
> 
> Did I miss something?
> 
> timo
> 
> On Thu, Sep 4, 2008 at 6:27 PM, Luke Daley <ld@... 
> <mailto:ld@...>> wrote:
> 
> 
>     On 05/09/2008, at 5:34 AM, Julien Ponge wrote:
> 
>         I have filled a JIRA issue
>         (http://jira.codehaus.org/browse/GROOVY-2892) a few weeks backs with
>         the idea of adding support for unless / if expressions to Groovy,
>         like:
> 
> 
>         Jochen suggested discussing this with the Groovy users community.
> 
>         What do you think?
> 
> 
>     +1000 for unless, except I think we should stick with pre statement
>     rather post statement. To be clear…
> 
>     unless (x > 10) doSomething()
> 
>     if (x < 10) triggerSomethingNasty()
> 
>     I actually prefer the ruby/perl style of having the condition
>     afterwards for single statement expressions, but since Groovy
>     already supports have the condition before the statement I would
>     advocate sticking with this.
> 
>     --
> 
>     LD.
> 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Russel Winder | 5 Sep 09:42

Re: [groovy-user] Support for unless / if expressions

On Thu, 2008-09-04 at 21:20 -0400, Martin C. Martin wrote:

[ . . . ]

> def myFunMethod(int foo) {
>    throw new RuntimeException("Arg must be positive") if (foo < 0)
>    ...

I guess we could get into the linguistics etc. here -- I am sure that if
an experiment were done we would find that native English speakers and
native German speakers have different views on this due to the different
structures of sentence in their natural languages.

For me though this is a throw statement so I expect the result to be
throwing an exception.  To find that the throws statement isn't actually
going to throw an exception, possibly most of the time, is a
comprehension problem for me.  True, this is a personal thing, but
without doing an experiment, we are doing advocacy decision making based
on personal experience. 

I agree that some functional programming languages, and indeed
mathematics, uses guards on the right in many contexts, however there is
a clear and obvious structure to when they are used.  This Perl/Ruby
style condition on the right is actually a very different sort of thing
since it is not part of a bigger construct.

[ . . . ]

> Of course, it's possible to use this to write confusing code.  Any 

I guess this is where I lean towards Python and away from Perl and Ruby.
Perl especially, at least reputedly, has attempted to make it a benefit
of the language that for any algorithmic construct there are multiple,
preferably more, ways of expressing the same thing.  Ruby, in trying to
be a better Perl, has gone the same route.

From the perspective of a C, C++, Fortran, Java, Python person, I prefer
my Groovy simple.  True, I can avoid using constructs in my own code,
the issue is working with other people's code.  Python often goes too
far in the simplicity stakes, indeed there are some decisions there I
disagree with -- cf. the conditional statement syntax which is ugly
because of the condition on the right ordering.  However, I favour
Pythonic simplicity over Perlesque complexity.

--

-- 
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077
Thomas R. Corbin | 17 Sep 18:23
X-Face

Re: [groovy-user] Support for unless / if expressions

On Friday 05 September 2008, Russel Winder said:
> On Thu, 2008-09-04 at 21:20 -0400, Martin C. Martin wrote:
>
> [ . . . ]
>
> > def myFunMethod(int foo) {
> >    throw new RuntimeException("Arg must be positive") if (foo < 0)
> >    ...
>
> I guess we could get into the linguistics etc. here -- I am sure that if
> an experiment were done we would find that native English speakers and
> native German speakers have different views on this due to the different
> structures of sentence in their natural languages.
>
> For me though this is a throw statement so I expect the result to be
> throwing an exception.  To find that the throws statement isn't actually
> going to throw an exception, possibly most of the time, is a
> comprehension problem for me.  True, this is a personal thing, but
> without doing an experiment, we are doing advocacy decision making based
> on personal experience.
>
> I agree that some functional programming languages, and indeed
> mathematics, uses guards on the right in many contexts, however there is
> a clear and obvious structure to when they are used.  This Perl/Ruby
> style condition on the right is actually a very different sort of thing
> since it is not part of a bigger construct.
>
> [ . . . ]
>
> > Of course, it's possible to use this to write confusing code.  Any
>
> I guess this is where I lean towards Python and away from Perl and Ruby.
> Perl especially, at least reputedly, has attempted to make it a benefit
> of the language that for any algorithmic construct there are multiple,
> preferably more, ways of expressing the same thing.  Ruby, in trying to
> be a better Perl, has gone the same route.
>
> From the perspective of a C, C++, Fortran, Java, Python person, I prefer
> my Groovy simple.  True, I can avoid using constructs in my own code,
> the issue is working with other people's code.  Python often goes too
> far in the simplicity stakes, indeed there are some decisions there I
> disagree with -- cf. the conditional statement syntax which is ugly
> because of the condition on the right ordering.  However, I favour
> Pythonic simplicity over Perlesque complexity.

Absolutely - the pythonic way is beautiful.   I fully lean towards python and 
away from ruby and VERY far away from perl.

--

-- 
Card #1. Uncle Bob’s Three Laws

• Write no production code except to pass a failing test.
• Write only enough of a test to demonstrate a failure.
• Write only enough production code to pass the test.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Russel Winder | 5 Sep 08:43

Re: [groovy-user] Support for unless / if expressions

On Thu, 2008-09-04 at 19:38 -0500, Stand Trooper wrote:
[ . . . ]

> As a pre-condition I don't see the potential for it.  I would suppose
> that if I used Ruby or Python that would be different.  I would need
> more of a reason to push for an additional reason to bloviate the
> language even more when I would look to an if...elseif...else or
> another conditional to push my objective.

I think you mean Perl and not Python.

> As for additions to the language, I don't have issues with that, but
> I've read the ticket and except in the case that Paul King put forth
> with the do...unless I don't see this as anything useful.  In all the
> years of programming I can tell you I've never once used a do...while
> outside of classrooms.

There are case but I can live without it.  The biggest problem is "test
in the middle" where you end up doing:

	while ( true ) {
		. . . 
		if ( condition ) { break ; }
		. . . 
	}

but I have never seen a nice solution to this problem.

> Are there other practical examples of this usage that might sway my
> opinion?  Otherwise, I just don't see it.  If it's to be added as
> syntactic sugar in place of an else statement, then I suppose that
> would be acceptable, but I don't see it as anything spectacular that
> an else couldn't do.
> 
> 
> Did I miss something?

I don't think so, I see no reason for having the constructs, and I
haven't seen a telling argument put forward yet.

--

-- 
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077
Ladislav Thon | 5 Sep 08:52

Re: [groovy-user] Support for unless / if expressions

There are case but I can live without it.  The biggest problem is "test
in the middle" where you end up doing:

       while ( true ) {
               . . .
               if ( condition ) { break ; }
               . . .
       }

but I have never seen a nice solution to this problem.

Ada has loop with condition in the middle. The syntax is ... tadaaa ... exit when <condition>;

Personally I like the <statement> if/unless <condition> in Ruby, as others do. Having it in Groovy would be nice for me, but not really a necessity.

LT
Russel Winder | 5 Sep 08:34

Re: [groovy-user] Support for unless / if expressions

On Fri, 2008-09-05 at 09:27 +1000, Luke Daley wrote:
> On 05/09/2008, at 5:34 AM, Julien Ponge wrote:
> 
> > I have filled a JIRA issue
> > (http://jira.codehaus.org/browse/GROOVY-2892) a few weeks backs with
> > the idea of adding support for unless / if expressions to Groovy,
> > like:
> >
> >
> > Jochen suggested discussing this with the Groovy users community.
> >
> > What do you think?
> 
> +1000 for unless, except I think we should stick with pre statement  
> rather post statement. To be clear…
> 
> unless (x > 10) doSomething()
> 
> if (x < 10) triggerSomethingNasty()

This is a very different proposal and one that works much better for me.
Clearly unless here is syntactic sugar for if ( ! ( ) ) but this is a
different sort of replication to the original proposal.

> I actually prefer the ruby/perl style of having the condition  
> afterwards for single statement expressions, but since Groovy already  
> supports have the condition before the statement I would advocate  
> sticking with this.

I suspect we are going to have to agree to disagree on this point about
where to put the condition, I find conditions after the expression to be
incomprehensible.

We agree though on sticking with what Groovy has :-) 
--

-- 
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077
TrnsltLife | 5 Sep 23:00
Favicon
Gravatar

Re: [groovy-user] Support for unless / if expressions


I'm OK with the idea of unless(condition){block}, but what would be the
syntax comparable to if(...){...} else if(...){...}?  

unless(...){...} else unless(...){...} else if(...){...}

Maybe we should allow
unless(...){...} otherwise unless(...){...} otherwise if(...){...}
otherwise{...}

Because I really don't like the sound of "else unless". On the other hand,
"else if" doesn't sound right until you're used to it, so maybe it doesn't
matter.

Jeremy
--

-- 
View this message in context: http://www.nabble.com/Support-for-unless---if-expressions-tp19321842p19339409.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Paul King | 6 Sep 01:12
Favicon
Gravatar

Re: [groovy-user] Support for unless / if expressions


I propose that instead of "else unless" we introduce
the "useless" operator and instead of "otherwise unless"
the "unwise" operator! :-)

Paul.

TrnsltLife wrote:
> I'm OK with the idea of unless(condition){block}, but what would be the
> syntax comparable to if(...){...} else if(...){...}?  
> 
> unless(...){...} else unless(...){...} else if(...){...}
> 
> Maybe we should allow
> unless(...){...} otherwise unless(...){...} otherwise if(...){...}
> otherwise{...}
> 
> Because I really don't like the sound of "else unless". On the other hand,
> "else if" doesn't sound right until you're used to it, so maybe it doesn't
> matter.
> 
> Jeremy

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Favicon

Re: [groovy-user] Support for unless / if expressions

TrnsltLife wrote:
> I'm OK with the idea of unless(condition){block}, but what would be the
> syntax comparable to if(...){...} else if(...){...}?  
> 
> unless(...){...} else unless(...){...} else if(...){...}
> 
> Maybe we should allow
> unless(...){...} otherwise unless(...){...} otherwise if(...){...}
> otherwise{...}
> 
> Because I really don't like the sound of "else unless". On the other hand,
> "else if" doesn't sound right until you're used to it, so maybe it doesn't
> matter.

unless...else is pretty much widely discouraged in the Ruby community 
because the meaning baffles most people. Generally unless is expected to 
only have a "then".

Besides, if you have an "unless...else", it would be better to flip the 
bodies and make it an if.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Luke Daley | 6 Sep 17:22

Re: [groovy-user] Support for unless / if expressions


On 06/09/2008, at 7:52 PM, Charles Oliver Nutter wrote:

> unless...else is pretty much widely discouraged in the Ruby  
> community because the meaning baffles most people.

Source?

--

LD.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Luke Daley | 6 Sep 17:35

Re: [groovy-user] Support for unless / if expressions


On 07/09/2008, at 1:22 AM, Luke Daley wrote:

>> unless...else is pretty much widely discouraged in the Ruby  
>> community because the meaning baffles most people.
>
> Source?

Just re-read, I first took it to mean that unless was frowned upon.  
Makes sense.

--

LD.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Favicon

Re: [groovy-user] Support for unless / if expressions

Luke Daley wrote:
> 
> On 07/09/2008, at 1:22 AM, Luke Daley wrote:
> 
>>> unless...else is pretty much widely discouraged in the Ruby community 
>>> because the meaning baffles most people.
>>
>> Source?
> 
> Just re-read, I first took it to mean that unless was frowned upon. 
> Makes sense.

Ahh, yes. Yeah, unless and until are both pretty popular in the Ruby 
world, and it's considered good form to use them rather than if 
!something or while !something.

I love them...they're one of the reasons I really like Ruby.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

phil swenson | 5 Sep 05:16

Re: [groovy-user] Support for unless / if expressions

I like the idea... probably because I've used Ruby quite a bit.  Let's face it, Groovy is pretty much a java-ized Ruby.

On Thu, Sep 4, 2008 at 1:34 PM, Julien Ponge <julien.ponge-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi everyone,

I have filled a JIRA issue
(http://jira.codehaus.org/browse/GROOVY-2892) a few weeks backs with
the idea of adding support for unless / if expressions to Groovy,
like:

doSomething() unless (x > 10)
(...)
triggerSomethingNasty() if (x < 10)

Jochen suggested discussing this with the Groovy users community.

What do you think?

--
http://izpack.org/
http://jpz-log.info/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Russel Winder | 5 Sep 08:38

Re: [groovy-user] Support for unless / if expressions

On Thu, 2008-09-04 at 21:16 -0600, phil swenson wrote:
> I like the idea... probably because I've used Ruby quite a bit.  Let's
> face it, Groovy is pretty much a java-ized Ruby.

No. JRuby is a Java-ized Ruby, Groovy is a dynamicized Java.  The
difference is a) not as trivial as it sounds, and b) very important.

For people wanting to work with Ruby on the JVM then JRuby is about as
ideal as it gets.  This is not the same thing as wanting a dynamic
language that is symbiotic with Java running on the JVM.

--

-- 
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077
Martin C. Martin | 5 Sep 13:02

Re: [groovy-user] Support for unless / if expressions


Russel Winder wrote:
> On Thu, 2008-09-04 at 21:16 -0600, phil swenson wrote:
>> I like the idea... probably because I've used Ruby quite a bit.  Let's
>> face it, Groovy is pretty much a java-ized Ruby.
> 
> No. JRuby is a Java-ized Ruby, Groovy is a dynamicized Java.  The
> difference is a) not as trivial as it sounds, and b) very important.
> 
> For people wanting to work with Ruby on the JVM then JRuby is about as
> ideal as it gets.  This is not the same thing as wanting a dynamic
> language that is symbiotic with Java running on the JVM.

I think Phil's point is that Groovy's syntax is very similar to Ruby's. 
  You can think of Groovy as starting with Ruby, and changing the syntax 
in minor ways to make it more friendly to Java users.  For example, you 
can pass closures by having them follow a method invocation:

foo(23) { println it }

rather than:

foo(23, {println it})

On the other hand, it delimits blocks using { and }, not "do" and "end."

Syntactically, Groovy is far closer to Ruby than to any other language, 
including Java.

Best,
Martin

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Graeme Rocher | 5 Sep 13:04
Gravatar

Re: [groovy-user] Support for unless / if expressions

On Fri, Sep 5, 2008 at 12:02 PM, Martin C. Martin
<martin@...> wrote:
>
>
> Russel Winder wrote:
>>
>> On Thu, 2008-09-04 at 21:16 -0600, phil swenson wrote:
>>>
>>> I like the idea... probably because I've used Ruby quite a bit.  Let's
>>> face it, Groovy is pretty much a java-ized Ruby.
>>
>> No. JRuby is a Java-ized Ruby, Groovy is a dynamicized Java.  The
>> difference is a) not as trivial as it sounds, and b) very important.
>>
>> For people wanting to work with Ruby on the JVM then JRuby is about as
>> ideal as it gets.  This is not the same thing as wanting a dynamic
>> language that is symbiotic with Java running on the JVM.
>
> I think Phil's point is that Groovy's syntax is very similar to Ruby's.  You
> can think of Groovy as starting with Ruby, and changing the syntax in minor
> ways to make it more friendly to Java users.  For example, you can pass
> closures by having them follow a method invocation:
>
> foo(23) { println it }
>
> rather than:
>
> foo(23, {println it})
>
> On the other hand, it delimits blocks using { and }, not "do" and "end."
>
> Syntactically, Groovy is far closer to Ruby than to any other language,
> including Java.

That point is highly debatable given that Groovy grammar is based off
of the Java 5 banner. It depends whether you writing Groovy like Java
or whether you write it in an idiomatic sense

Cheers

>
> Best,
> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>
>

--

-- 
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Graeme Rocher | 5 Sep 13:05
Gravatar

Re: [groovy-user] Support for unless / if expressions

On Fri, Sep 5, 2008 at 12:04 PM, Graeme Rocher <graeme@...> wrote:
> On Fri, Sep 5, 2008 at 12:02 PM, Martin C. Martin
> <martin@...> wrote:
>>
>>
>> Russel Winder wrote:
>>>
>>> On Thu, 2008-09-04 at 21:16 -0600, phil swenson wrote:
>>>>
>>>> I like the idea... probably because I've used Ruby quite a bit.  Let's
>>>> face it, Groovy is pretty much a java-ized Ruby.
>>>
>>> No. JRuby is a Java-ized Ruby, Groovy is a dynamicized Java.  The
>>> difference is a) not as trivial as it sounds, and b) very important.
>>>
>>> For people wanting to work with Ruby on the JVM then JRuby is about as
>>> ideal as it gets.  This is not the same thing as wanting a dynamic
>>> language that is symbiotic with Java running on the JVM.
>>
>> I think Phil's point is that Groovy's syntax is very similar to Ruby's.  You
>> can think of Groovy as starting with Ruby, and changing the syntax in minor
>> ways to make it more friendly to Java users.  For example, you can pass
>> closures by having them follow a method invocation:
>>
>> foo(23) { println it }
>>
>> rather than:
>>
>> foo(23, {println it})
>>
>> On the other hand, it delimits blocks using { and }, not "do" and "end."
>>
>> Syntactically, Groovy is far closer to Ruby than to any other language,
>> including Java.
>
> That point is highly debatable given that Groovy grammar is based off
> of the Java 5 banner. It depends whether you writing Groovy like Java
> or whether you write it in an idiomatic sense

Sorry Java 5 grammar that is. Too many hours writing the new Grails book :-(

>
> Cheers
>
>>
>> Best,
>> Martin
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>

--

-- 
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Martin C. Martin | 5 Sep 13:19

Re: [groovy-user] Support for unless / if expressions

Very true, good point.  Groovy allows either Java-style or Ruby-style.

Graeme Rocher wrote:
> On Fri, Sep 5, 2008 at 12:02 PM, Martin C. Martin
> <martin@...> wrote:
>>
>> Russel Winder wrote:
>>> On Thu, 2008-09-04 at 21:16 -0600, phil swenson wrote:
>>>> I like the idea... probably because I've used Ruby quite a bit.  Let's
>>>> face it, Groovy is pretty much a java-ized Ruby.
>>> No. JRuby is a Java-ized Ruby, Groovy is a dynamicized Java.  The
>>> difference is a) not as trivial as it sounds, and b) very important.
>>>
>>> For people wanting to work with Ruby on the JVM then JRuby is about as
>>> ideal as it gets.  This is not the same thing as wanting a dynamic
>>> language that is symbiotic with Java running on the JVM.
>> I think Phil's point is that Groovy's syntax is very similar to Ruby's.  You
>> can think of Groovy as starting with Ruby, and changing the syntax in minor
>> ways to make it more friendly to Java users.  For example, you can pass
>> closures by having them follow a method invocation:
>>
>> foo(23) { println it }
>>
>> rather than:
>>
>> foo(23, {println it})
>>
>> On the other hand, it delimits blocks using { and }, not "do" and "end."
>>
>> Syntactically, Groovy is far closer to Ruby than to any other language,
>> including Java.
> 
> That point is highly debatable given that Groovy grammar is based off
> of the Java 5 banner. It depends whether you writing Groovy like Java
> or whether you write it in an idiomatic sense
> 
> Cheers
> 
>> Best,
>> Martin
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>>
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Guillaume Laforge | 5 Sep 13:08
Gravatar

Re: [groovy-user] Support for unless / if expressions

On Fri, Sep 5, 2008 at 1:02 PM, Martin C. Martin
<martin@...> wrote:
> [...]
> Syntactically, Groovy is far closer to Ruby than to any other language,
> including Java.

It's more about "some" APIs which look like Ruby's, but the syntax is
firmly in the Java ground.

--

-- 
Guillaume Laforge
Groovy Project Manager
G2One, Inc. Vice-President Technology
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 5 Sep 14:02
Gravatar

Re: [groovy-user] Support for unless / if expressions

Martin C. Martin schrieb:
[...]
>  For example, you 
> can pass closures by having them follow a method invocation:
> 
> foo(23) { println it }
> 
> rather than:
> 
> foo(23, {println it})

in Java we have for example:

synchronized(foo) {bar();}

which very much looks like the first form. If you think about it, then 
syntactically a for- or while-loop do like that too. The simple if 
without else fits here in too. So let us see these as of the structure 
"method arguments". then there is for example if-else...

if (condition) {
   ...
} else {
   ...
}

which would be "method arguments else arguments" or probably: 
"method(arguments,method(arguments))" it could be thought that the else 
is executed first, without actually executing the block, but building 
another argument for the if method call. But how is that method 
detected? If else where a symbol it would be "method arguments", where 
one of the arguments would be the else symbol and the if method would 
have to find that symbol... throw-catch could be explained like this as 
well.... but unless...

foo() unless bar()

that is like "method arguments unless method arguments". If we go the 
symbol way, then foo() would be in fact: foo('unless,bar()), with unless 
being a symbol and {bar()} a block to be executed if the symbol is part 
of the arguments... but that is not what we want. We don't want foo() to 
handle any symbol that does not exist anyway. We don't want foo to 
handle any block/Closure that is returned by the method call "unless 
bar()". foo() should remain untouched.

But that means we reverse the usual ordering of things....

foo() unless bar()

becomes

if (bar()) {
} else {
   foo()
}

eben if we say unless is "methodcall unless condition", and say we have 
a structure consisting of three parts and look at other such structure 
like if-else: "methodcall else methodcall" then we still have a 
different order, because the first method call in if-else is executed 
first and the second only if the condition is not true. In case of 
unless the second method call is executed first and the first method 
call is executed only if the condition is met. Other 3-part structures 
could bee try-catch, but again, the first part here is executed first. 
And even in do-while we have this.

What I want to say in short: This kind of evaluation order is nowhere to 
be found in the existing language... it would be an alien element.

If it stays the only such element, then I am against adding it. If we 
intend to add multiple such elements (where the evaluation order is 
reversed) then it is something to discuss again.

The Ada was named... from 
http://en.wikibooks.org/wiki/Ada_Programming/Control

> Exit_Loop :
>    loop
>       X := Calculate_Something;
>       exit Exit_Loop when X > 5;
>       Do_Something (X);
>    end loop  Exit_Loop;

the same in Groovy would be:

Exit_Loop: while (true) {
   def X = Calculate_Something()
   if (X>5) break Exit_Loop
   Do_Something (X)
}

not only do we start with a keyword here, this construct is also special 
to loops and labels. That is far from the semantics of "unless"

The suggested

do foo() unless bar()

would be much more fitting for Groovy, because it fits the structure 
"method arguments", where foo(), the symbol unless and bar() are the 
arguments to the method do. The matter that we don't support symbols is 
not important here, important is that this fact is an implementation 
detail and hidden by the "do"-method.

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Martin C. Martin | 5 Sep 14:08

Re: [groovy-user] Support for unless / if expressions

Good point, which is no doubt what inspired Ruby.  So its more general 
and less Ruby-specific than I was thinking.

Best,
Martin

Jochen Theodorou wrote:
> Martin C. Martin schrieb:
> [...]
>>  For example, you can pass closures by having them follow a method 
>> invocation:
>>
>> foo(23) { println it }
>>
>> rather than:
>>
>> foo(23, {println it})
> 
> in Java we have for example:
> 
> synchronized(foo) {bar();}
> 
> which very much looks like the first form. If you think about it, then 
> syntactically a for- or while-loop do like that too. The simple if 
> without else fits here in too. So let us see these as of the structure 
> "method arguments". then there is for example if-else...
> 
> if (condition) {
>   ...
> } else {
>   ...
> }
> 
> which would be "method arguments else arguments" or probably: 
> "method(arguments,method(arguments))" it could be thought that the else 
> is executed first, without actually executing the block, but building 
> another argument for the if method call. But how is that method 
> detected? If else where a symbol it would be "method arguments", where 
> one of the arguments would be the else symbol and the if method would 
> have to find that symbol... throw-catch could be explained like this as 
> well.... but unless...
> 
> foo() unless bar()
> 
> that is like "method arguments unless method arguments". If we go the 
> symbol way, then foo() would be in fact: foo('unless,bar()), with unless 
> being a symbol and {bar()} a block to be executed if the symbol is part 
> of the arguments... but that is not what we want. We don't want foo() to 
> handle any symbol that does not exist anyway. We don't want foo to 
> handle any block/Closure that is returned by the method call "unless 
> bar()". foo() should remain untouched.
> 
> But that means we reverse the usual ordering of things....
> 
> foo() unless bar()
> 
> becomes
> 
> if (bar()) {
> } else {
>   foo()
> }
> 
> eben if we say unless is "methodcall unless condition", and say we have 
> a structure consisting of three parts and look at other such structure 
> like if-else: "methodcall else methodcall" then we still have a 
> different order, because the first method call in if-else is executed 
> first and the second only if the condition is not true. In case of 
> unless the second method call is executed first and the first method 
> call is executed only if the condition is met. Other 3-part structures 
> could bee try-catch, but again, the first part here is executed first. 
> And even in do-while we have this.
> 
> What I want to say in short: This kind of evaluation order is nowhere to 
> be found in the existing language... it would be an alien element.
> 
> If it stays the only such element, then I am against adding it. If we 
> intend to add multiple such elements (where the evaluation order is 
> reversed) then it is something to discuss again.
> 
> The Ada was named... from 
> http://en.wikibooks.org/wiki/Ada_Programming/Control
> 
>> Exit_Loop :
>>    loop
>>       X := Calculate_Something;
>>       exit Exit_Loop when X > 5;
>>       Do_Something (X);
>>    end loop  Exit_Loop;
> 
> the same in Groovy would be:
> 
> Exit_Loop: while (true) {
>   def X = Calculate_Something()
>   if (X>5) break Exit_Loop
>   Do_Something (X)
> }
> 
> not only do we start with a keyword here, this construct is also special 
> to loops and labels. That is far from the semantics of "unless"
> 
> The suggested
> 
> do foo() unless bar()
> 
> would be much more fitting for Groovy, because it fits the structure 
> "method arguments", where foo(), the symbol unless and bar() are the 
> arguments to the method do. The matter that we don't support symbols is 
> not important here, important is that this fact is an implementation 
> detail and hidden by the "do"-method.
> 
> bye blackdrag
> 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Erick Erickson | 5 Sep 15:08

Re: [groovy-user] Support for unless / if expressions

I'm firmly on the *no* side on this one. Don't give me yet
another way to do something that's already expressed in
the language. Pretty quickly you wind up with a different
dialect for each programmer, depending upon her
predilections.

We can argue for years about intangibles like "expressiveness"
and "clarity", when in reality no programming language is clear.
They're all learned perversions <G>. Far too often these are
code-words for "don't make me learn anything new".

I far prefer a minimal number of ways to perform an action, even
when it does drive me crazy for a while learning the new language.
At least I only have to learn it once rather than re-learn it every
time I see someone else's code....

Best
Erick

On Fri, Sep 5, 2008 at 8:08 AM, Martin C. Martin <martin-i949vRgkDusU9HFyfbEPPQC/G2K4zDHf@public.gmane.org> wrote:
Good point, which is no doubt what inspired Ruby.  So its more general and less Ruby-specific than I was thinking.

Best,
Martin


Jochen Theodorou wrote:
Martin C. Martin schrieb:
[...]
 For example, you can pass closures by having them follow a method invocation:

foo(23) { println it }

rather than:

foo(23, {println it})

in Java we have for example:

synchronized(foo) {bar();}

which very much looks like the first form. If you think about it, then syntactically a for- or while-loop do like that too. The simple if without else fits here in too. So let us see these as of the structure "method arguments". then there is for example if-else...

if (condition) {
 ...
} else {
 ...
}

which would be "method arguments else arguments" or probably: "method(arguments,method(arguments))" it could be thought that the else is executed first, without actually executing the block, but building another argument for the if method call. But how is that method detected? If else where a symbol it would be "method arguments", where one of the arguments would be the else symbol and the if method would have to find that symbol... throw-catch could be explained like this as well.... but unless...

foo() unless bar()

that is like "method arguments unless method arguments". If we go the symbol way, then foo() would be in fact: foo('unless,bar()), with unless being a symbol and {bar()} a block to be executed if the symbol is part of the arguments... but that is not what we want. We don't want foo() to handle any symbol that does not exist anyway. We don't want foo to handle any block/Closure that is returned by the method call "unless bar()". foo() should remain untouched.

But that means we reverse the usual ordering of things....

foo() unless bar()

becomes

if (bar()) {
} else {
 foo()
}

eben if we say unless is "methodcall unless condition", and say we have a structure consisting of three parts and look at other such structure like if-else: "methodcall else methodcall" then we still have a different order, because the first method call in if-else is executed first and the second only if the condition is not true. In case of unless the second method call is executed first and the first method call is executed only if the condition is met. Other 3-part structures could bee try-catch, but again, the first part here is executed first. And even in do-while we have this.

What I want to say in short: This kind of evaluation order is nowhere to be found in the existing language... it would be an alien element.

If it stays the only such element, then I am against adding it. If we intend to add multiple such elements (where the evaluation order is reversed) then it is something to discuss again.

The Ada was named... from http://en.wikibooks.org/wiki/Ada_Programming/Control

Exit_Loop :
  loop
     X := Calculate_Something;
     exit Exit_Loop when X > 5;
     Do_Something (X);
  end loop  Exit_Loop;

the same in Groovy would be:

Exit_Loop: while (true) {
 def X = Calculate_Something()
 if (X>5) break Exit_Loop
 Do_Something (X)
}

not only do we start with a keyword here, this construct is also special to loops and labels. That is far from the semantics of "unless"

The suggested

do foo() unless bar()

would be much more fitting for Groovy, because it fits the structure "method arguments", where foo(), the symbol unless and bar() are the arguments to the method do. The matter that we don't support symbols is not important here, important is that this fact is an implementation detail and hidden by the "do"-method.

bye blackdrag


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email



Ladislav Thon | 5 Sep 15:25

Re: [groovy-user] Support for unless / if expressions

The Ada was named...

I mentioned Ada because I replied about loop with condition in the middle :-)

It's clear to me that "postfix" if/unless is not consistent with Groovy at all. I only thought it would be nice to have for internal DSLs created in Groovy. All that said, I believe everybody can live without it.

LT
Jochen Theodorou | 5 Sep 15:44
Gravatar

Re: [groovy-user] Support for unless / if expressions

Ladislav Thon schrieb:
>     The Ada was named...
> 
> I mentioned Ada because I replied about loop with condition in the 
> middle :-)

I too name Ada from time to time... but I learned Ada'95 to know as a 
language that has very strange object orientation, that is very noisy 
and has an interesting sub type concept. Who ever thought of mentioning 
a an object type in the method signature makes the method part of the 
object and included the return type there needs to be thanked, because 
that was the first time I realized that people working with a 
programming language for years still may not know what it actually does.

> It's clear to me that "postfix" if/unless is not consistent with Groovy 
> at all. I only thought it would be nice to have for internal DSLs 
> created in Groovy. All that said, I believe everybody can live without it.

I think it needs either to be part of a more general concept or should 
not added in the way Ruby is using that. In Ruby the evaluation order is 
probably different... I don't know. I know that you can define binary 
operator in Ruby and unless could be seen as such an operator. Groovy 
lacks this... so a more general concept would for example be to add 
custom binary operators to Groovy, that would then enable unless.

Looking at http://phrogz.net/ProgrammingRuby/language.html#table_18.4 
unless is indeed an operator. Only that we don't have any word based 
operator in Groovy... none besides instanceof

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Chris Reeves | 5 Sep 06:29

Re: [groovy-user] Support for unless / if expressions

I'm for it. Definitely for unless as a prefixed conditional because, as others 
have pointed out, it is easier to read for code review than if (!something). 
I'm for the postfixed versions of both because there are certain instances, 
again as others have pointed out, where code is easier to read when a 
condition follows a statement. It takes some getting used to, but really helps 
readability in certain situations.

Thanks, Chris

On Thursday 04 September 2008 15:34:42 Julien Ponge wrote:
> Hi everyone,
>
> I have filled a JIRA issue
> (http://jira.codehaus.org/browse/GROOVY-2892) a few weeks backs with
> the idea of adding support for unless / if expressions to Groovy,
> like:
>
> doSomething() unless (x > 10)
> (...)
> triggerSomethingNasty() if (x < 10)
>
> Jochen suggested discussing this with the Groovy users community.
>
> What do you think?

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Russel Winder | 5 Sep 08:40

Re: [groovy-user] Support for unless / if expressions

On Fri, 2008-09-05 at 00:29 -0400, Chris Reeves wrote:
> I'm for it. Definitely for unless as a prefixed conditional because, as others 
> have pointed out, it is easier to read for code review than if (!something). 
> I'm for the postfixed versions of both because there are certain instances, 
> again as others have pointed out, where code is easier to read when a 
> condition follows a statement. It takes some getting used to, but really helps 
> readability in certain situations.

"It takes some getting used to" is exactly why I think this idea of
conditions after the expression is a bad idea.

To date there have been no examples of where readability is improved
just a belief that because Perl and Ruby have it then Groovy should have
it.  I propose that actually Perl and Ruby are wrong to have this
feature and that they should remove it.

--

-- 
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077
Chris Reeves | 5 Sep 15:39

Re: [groovy-user] Support for unless / if expressions

To be fair, closures also take some getting used to, as do several other 
groovy constructs.

As far as readability, the case for using unless in place of if (!something) 
is better for non-java/groovy/c-like programmers everywhere, as they tend not 
to see the !. I work with foxpro, basic, and cobol programmers regularly. To 
them

if (!(somemap.mykey instanceof String)) {
}

is much more confusing than

unless (somemap.mykey instanceof String) {
}

From a human usability standpoint, there are three extra meaningful tokens in 
the import part of the conditional that have to be processed before one can 
grasp exactly what it does. A long-time java programmer can read that fairly 
easily, but those without the c-like background tend to trip over it. I 
suppose you could eliminate the extra (), but that would make the condition a 
little less clear.

As far as postfixed conditions go, that is really just a personal preference, 
I suppose. There are instances in my code, where it would be easier for me to 
read later on, if I had postfixed conditions. I tend to read terse code more 
easily, and if I have a condition, I always put the branch blocks in {}, so 
every condition ends up being at least three lines. With a postfixed simple 
condition, the way *I* used it in ruby anyways, short conditional tests with 
one-line branch blocks end up being one line.

Then again, personal preference of a few converts from other languages is not 
exactly a valid reason to add a language construct, unless there are more than 
just a few. ( a postfixed condition ;) )

Thanks, Chris

On Friday 05 September 2008 02:40:47 Russel Winder wrote:
> On Fri, 2008-09-05 at 00:29 -0400, Chris Reeves wrote:
> > I'm for it. Definitely for unless as a prefixed conditional because, as
> > others have pointed out, it is easier to read for code review than if
> > (!something). I'm for the postfixed versions of both because there are
> > certain instances, again as others have pointed out, where code is easier
> > to read when a condition follows a statement. It takes some getting used
> > to, but really helps readability in certain situations.
>
> "It takes some getting used to" is exactly why I think this idea of
> conditions after the expression is a bad idea.
>
> To date there have been no examples of where readability is improved
> just a belief that because Perl and Ruby have it then Groovy should have
> it.  I propose that actually Perl and Ruby are wrong to have this
> feature and that they should remove it.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 5 Sep 15:55
Gravatar

Re: [groovy-user] Support for unless / if expressions

Chris Reeves schrieb:
> To be fair, closures also take some getting used to, as do several other 
> groovy constructs.
> 
> As far as readability, the case for using unless in place of if (!something) 
> is better for non-java/groovy/c-like programmers everywhere, as they tend not 
> to see the !. I work with foxpro, basic, and cobol programmers regularly. To 
> them
> 
> if (!(somemap.mykey instanceof String)) {
> }
> 
> is much more confusing than
> 
> unless (somemap.mykey instanceof String) {
> }

agreed, but I think instaceof is the only one that looks as bad as that. 
So the trained Java programmer keeps an extra eye on the ! when 
instanceof is used.

[...]
> As far as postfixed conditions go, that is really just a personal preference, 
> I suppose. There are instances in my code, where it would be easier for me to 
> read later on, if I had postfixed conditions. I tend to read terse code more 
> easily, and if I have a condition, I always put the branch blocks in {}, so 
> every condition ends up being at least three lines. With a postfixed simple 
> condition, the way *I* used it in ruby anyways, short conditional tests with 
> one-line branch blocks end up being one line.

I have a certain style of using early returns... that means:

def method(...) {
   if (!condition1) return ...
   ...
}

instead of

def method(...) {
   if (condition1) {
     ...
   } else {
     return ...
  }
}

These guarding if-statements for the early return are usually one-liners 
and if not I usually define a helper method or rework the whole 
structure. What I want to say is, that in this programming style you 
don't get much advantage by using unless in terms of LOC.

> Then again, personal preference of a few converts from other languages is not 
> exactly a valid reason to add a language construct, unless there are more than 
> just a few. ( a postfixed condition ;) )

if it is a very common construct, then it is ok to add it. But "unless" 
is not common. I don't know about the English native speakers, but I use 
unless rarely. In my mother language too.... If I use it, then because I 
had an idea in the middle of the sentence ;)

bye blackdrag

--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Detering Dirk | 8 Sep 12:02

RE: [groovy-user] Support for unless / if expressions

We should be aware that  "if not a is greater than b, than do that"
is a very formal speech.

What is ok in a programmer's context, as for us a PL code is a formal
specification of a program.

But what about DSL's ?
I can imagine that in some domain contexts it could indeed be 
useful to have the 'guard' style.

And: I have used trailing conditions and unless indeed in my Ruby code,
somehow like this:

     doSomething()
     doAnotherThing() unless isPreviewMode()
     doLastThing()

So the emphasis is on the job steps, which will *normally be done*
in this order, not on the condition, which *sometimes* will change the 
processing flow.
Pre or post condition is therefore a matter of relation between 
"typical" and "exception".

I'm always annoyed reading code like this:

  doSomething()
  if noError()
     doAnotherThing()
  if successfull()
     doLastThing()         

It breaks the reading of the standard process flow, which will be
"no error" and "successful" and put too much emphasis on the exceptional
cases.

And for natural languages:

Russel Winder wrote:

> I guess we could get into the linguistics etc. here -- 
> I am sure that if an experiment were done we would find 
> that native English speakers and native German speakers have 
> different views on this due to the different structures of 
> sentence in their natural languages.

I think that is not true for where to put conditionals.
The only difference may be, that we Germans don't have such 
a clear single word. 
(dict.leo.org:   unless =>
 .  außer dass
 .  es sei denn, dass
 .  sofern nicht
 .  wenn nicht )

Jochen Theodorou wrote:
> But "unless" is not common. I don't know about the English native 
> speakers, but I use unless rarely. In my mother language 
> too.... If I use it, then because I had an idea in the middle 
> of the sentence ;)

I must admit that in spoken language I use it often enough,
as it changes the emphasis.

A: "We never gonna survive!"
B: "Yes, we never gonna survive unless we're a little crazy"
(Remind: Seal 1990 ;-) )

In a naturally spoken context, a leading "unless" feels uncommon 
and formal for me, and trailing conditions seem very common:
"I will come tomorrow if I will get a cheap ticket" a.s.o. 

Example: Quote from the GPL:
-----
You may make, run and propagate covered works that you do not 
convey, without conditions so long as your license otherwise 
remains in force. You may convey covered works to others for 
the sole purpose of having them make modifications exclusively 
for you, or provide you with facilities for running those works, 
provided that you comply with the terms of this License in conveying 
all material for which you do not control copyright. 
------

Replace "so long as" and "provided that" with the simple "if"
and you have trailing conditions.

RESULT:  I would second 'unless' as well as trailing conditions
as they make things more flexible and readable in some groovy contexts.
(I would not cry if it would be dropped).

KR
Det

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jochen Theodorou | 9 Sep 17:53
Gravatar

Re: [groovy-user] Support for unless / if expressions

Detering Dirk schrieb:
[...]
> But what about DSL's ?
> I can imagine that in some domain contexts it could indeed be 
> useful to have the 'guard' style.

if you can, then give an example... not that I doubt, that you will find 
one, but because to know what we are actually talking about.

> And: I have used trailing conditions and unless indeed in my Ruby code,
> somehow like this:
> 
>      doSomething()
>      doAnotherThing() unless isPreviewMode()
>      doLastThing()

that's the unless-operator again... which is different from the unless 
statement.

My "problem" with this is, that this kind of operator is a whole new 
concept... one that is not present in current groovy. Why is it a new 
concept? Because:

expression op expression

is usually executed by evaluating both expressions and then applying the 
operator. In case of unless we do not want to evaluate the left 
expression under certain circumstances. This kind of behaviour can be 
found in loops and closures... which more or less means that the left 
side should be a closure/block.

>      doSomething()
>      {doAnotherThing()} unless isPreviewMode()
>      doLastThing()

Would that do? I am not sure, since the curly braces disrupt the 
reading. There is also the variant with a simple method:

>      doSomething()
>      {doAnotherThing()}.unless isPreviewMode()
>      doLastThing()

which would be an API change only.

> So the emphasis is on the job steps, which will *normally be done*
> in this order, not on the condition, which *sometimes* will change the 
> processing flow.
> Pre or post condition is therefore a matter of relation between 
> "typical" and "exception".

The same arguments you give here for unless do also apply to "if" and 
"while"... "doSomething() while shouldContinue()" seems to be equally 
reasonable to me.

> I'm always annoyed reading code like this:
> 
>   doSomething()
>   if noError()
>      doAnotherThing()
>   if successfull()
>      doLastThing()         
> 
> It breaks the reading of the standard process flow, which will be
> "no error" and "successful" and put too much emphasis on the exceptional
> cases.

probably that is because it is "if" and not "if then" as in some other 
languages. think of "if no error then do another thing" and "do another 
thing if no error". But probably the naming of the method would have 
been different: "do another thing if not in error state".

> And for natural languages:
> 
> Russel Winder wrote:
> 
>> I guess we could get into the linguistics etc. here -- 
>> I am sure that if an experiment were done we would find 
>> that native English speakers and native German speakers have 
>> different views on this due to the different structures of 
>> sentence in their natural languages.
> 
> I think that is not true for where to put conditionals.
> The only difference may be, that we Germans don't have such 
> a clear single word. 
> (dict.leo.org:   unless =>
>  .  außer dass
>  .  es sei denn, dass
>  .  sofern nicht
>  .  wenn nicht )

ah yes... german... languages of poets, because of it's great variety 
and creative usages of words... which is driving German learning people 
really nuts. Anyway I think all of these could be used at the beginning 
and in the middle...well, excluding "es sei denn, dass", which needs to 
be in the middle I think.

bye blackdrag
--

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Detering Dirk | 10 Sep 12:30

RE: [groovy-user] Support for unless / if expressions

Hi Jochen,

> if you can, then give an example... not that I doubt, that 
> you will find one, but because to know what we are actually 
> talking about.

Well, that is ... nah, OK, let's be spontaneously creative:

newJobDefinitionTable("Account calculation") {

//  Jobsteps  |  message       | guards                 |  Comment

prepareContext()                 unless ( isPrepared  )       
runStep_01       "Fancy Text"     
runStep_02       "#-${account}"                      
runStep_03()                     
runStep_04()                     if ( shallFinalise )   // Only for 
cleanupLog()                     unless ( error )

}

Just a quick draft.

> that's the unless-operator again... which is d