RCR: Comparing multiple values

  • Thread starter Kristof Bastiaensen
  • Start date
K

Kristof Bastiaensen

Hi list,

wouldn't it be nice to be able to compare multiple values?
for example:
x, y == 1, 2
#equivalent to x == 1 && y == 2

right now you can do
[x, y] == [1, 2]
but IMHO it doesn't look so nice

Kristof
 
S

Szymon Drejewicz

Kristof said:
wouldn't it be nice to be able to compare multiple values?
for example:
x, y == 1, 2
#equivalent to x == 1 && y == 2

For me that notation has one drawback: what should happend if
y = 5
and you'll write
[x, y == 1, 2]
?

Is it an array with three elements
[x, false, 2]
?

or an array with one element which has value of "multiple comaprision"
[false]
?
 
J

Joel VanderWerf

Szymon said:
Kristof Bastiaensen wrote:

wouldn't it be nice to be able to compare multiple values?
for example:
x, y == 1, 2
#equivalent to x == 1 && y == 2


For me that notation has one drawback: what should happend if
y = 5
and you'll write
[x, y == 1, 2]
?

Is it an array with three elements
[x, false, 2]
?

or an array with one element which has value of "multiple comaprision"
[false]
?

Not sure that this shows anything, but the current behavior for =
instead of == is:

[x, y = 1, 2]
=> [0, 1, 2]

So it looks like the array context overrides the parallel assignment?

It's not a good analogy, though, because == is a method and = is not.
 
G

gabriele renzi

il Mon, 26 Apr 2004 08:39:26 +0200, Szymon Drejewicz
Kristof said:
wouldn't it be nice to be able to compare multiple values?
for example:
x, y == 1, 2
#equivalent to x == 1 && y == 2

For me that notation has one drawback: what should happend if
y = 5
and you'll write
[x, y == 1, 2]
?

Is it an array with three elements
[x, false, 2]
?

or an array with one element which has value of "multiple comaprision"
[false]
?

It should be just 'false', as expressed in the first message (i.e.
x==1 and 5==2)

I mean, if we consider ',' as an expression that creates a list
object, than 1,2 == could be just some kind of sugar.
But chosing some kind of precedence would probably be impossible, or
wqould break many of the actual behaviours.
 
K

Kristof Bastiaensen

For me that notation has one drawback: what should happend if
y = 5
and you'll write
[x, y == 1, 2]
?

Is it an array with three elements
[x, false, 2]
?

or an array with one element which has value of "multiple comaprision"
[false]
?

That would be a problem indeed. Note that the same aplies
to method calling: foo(a, b == 1, 2)

The expansion of (x, y == 1, 2) should then occur only outside
parameter lists.
It should be just 'false', as expressed in the first message (i.e.
x==1 and 5==2)

It surely shouldn't be false, because that would disable
comparisons inside parameter lists.
I mean, if we consider ',' as an expression that creates a list
object, than 1,2 == could be just some kind of sugar.
But chosing some kind of precedence would probably be impossible, or
wqould break many of the actual behaviours.

I wouldn't consider ',' as an expression that creates any object,
but rather as a way to pass multiple arguments to methods and
continuations. This is close to the way it actually is in Ruby.

This would be a syntax extension. However (x, y == exp1, exp2)
would be different from (x == exp1 && y == exp2) because it would
evaluate the parameters first, and then compare the results.

Another construct that might be useful:
x, y += 1, 2
#(meaning x += 1; y += 2)
 
K

Kristof Bastiaensen

Not sure that this shows anything, but the current behavior for =
instead of == is:

[x, y = 1, 2]
=> [0, 1, 2]

So it looks like the array context overrides the parallel assignment?
It's not a good analogy, though, because == is a method and = is not.

I think the analogy is good enough. Ruby already does some method
reordering on Arithmetic Operators, so why not on this one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top