[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: alternatives to ? : contruct

Neville Burnell

5/12/2005

I'd be happy to see a "wordy" version of the ternary operator like so:

a = (y==z) then b else c

-----Original Message-----
From: Mark Hubbart [mailto:discordantus@gmail.com]
Sent: Thursday, 12 May 2005 9:42 AM
To: ruby-talk ML
Subject: Re: alternatives to ? : contruct

On 5/11/05, gwtmp01@mac.com <gwtmp01@mac.com> wrote:
> On May 11, 2005, at 4:07 PM, Jason Foreman wrote:
> >
> > I'd prefer it with less ;'s as well:
> >
> > a = if (y==z): b else c end
>
> Is this really better than:
>
> a = (y==z) ? b : c
>
> ?

A little, for readability. I like using actual words as operators
whenever possible. This is, of course, excepting symbolic operators like
+ and -, which I learned back in first grade, and carry as much meaning
as actual words.

> The tertiary operator ?: can be used if you want something terse and
> "if then else end" if you want something more verbose. I'm not sure I

> see the need for yet another variation.

First of all, it's the ternary operator :) As for the "if then else end"
statement, the dangling "end" at the... uh, *end*, is a bit of an
eyesore. It's what makes me shy away from using inline conditionals like
that.

I've thought a couple times that it would be nice to have something like
"otherwise" available. I wonder if it would be too difficult for the
parser to parse these two snippets the same:

----
a = if (y==z)
b
else
c
end
----
a = if (y==z) then b else c
----

That is, if else is on the same line as the code to execute if true,
with no semicolon, the "end" can be dropped. Or this, instead:

----
a = b if y==z else c
----

Can the parser definitively detect that the if...else is being used as a
statement modifier in this case?

Just some thoughts off the top of my head, here. I could easily waffle
on them if a good case is made against it.

cheers,
Mark



1 Answer

Martin DeMello

5/12/2005 4:59:00 AM

0

Neville Burnell <Neville.Burnell@bmsoft.com.au> wrote:
> I'd be happy to see a "wordy" version of the ternary operator like so:
>
> a = (y==z) then b else c

(y==z).then { b }.else { c } is doable, if the extra braces don't spoil
it for you.

martin