views:

365

answers:

6

An idle question on language design, see "Does C# have a right hand if like Perl".

For example, in recent C family languages

z = foo ( a, b, c );

a is evaluated, then b, then c, so left-to-right.

z = a ? b : c;

a is evaluated, then either b or c, so left-to-right order.

In Python, you write a conditional expression as

z = b if a else c

so the order is the same - a,then b or c, but it's not left-to-right.

Strict left-to-right ordering was put into Java to simplify the language; ordering is implementation dependent in C or C++.

A: 

no, but you're obviously looking for something.

Try this

McAden
No, it really is just idle curiosity. I don't find Perl readable enough to want to duplicate it, but am quite happy in Python, which does have a right-hand if in expressions.
Pete Kirkham
I had just read that thread you linked and the one before it, I assumed you were the same guy - didn't actually look at the name asking the question. You can use the same method to run python on C# though.
McAden
A: 

C# has the ternary operator:

result = a == b ? c : d

is equivalent to

if (a == b)
    result = c;
else
    result = d;

It's not strictly right to left, but it does reduce the amount of needed code.

Robert Harvey
Note that you can also do (b ? a : b) = c;
nos
um, I don't think so. The syntax in C# is pretty specific.
Robert Harvey
+3  A: 

Assignment is (sort of) right to left:

int a = b + c;

b+c gets evaluated, then assigned to a.


In general, though, the C# designers have purposely tried to keep most things left->right. A great example is LINQ. Here, instead of going with the traditional SQL ordering (SELECT XXX FROM XXX), they purposely reordered the query to be more left->right.

// Note the left->right ordering:
var results = from member in collection where member.element == condition select member;

// Which is equivelent to:
var resultsNonLinq = collection.Where().Select();

This is part of why I like C# - the consistency in the language is very refreshing, especially when compared to some languages like perl where there is purposely many ways of doing simple things.

Reed Copsey
The reason behind the reverse-SQL in LINQ is better auto-completion. When 'from` is first, Intellisense can be used for the 'where' and 'select' clause, whereas this would not be possible for the normal order as 'from' comes after 'select' and 'where' and 'from' is where the types to be used are filled in.
JulianR
That is ONE reason. There are several good reasons why we chose this order. Another is that it simplifies the variable scoping rules. Another is that it follows the order in which the operations happen. And so on.
Eric Lippert
A: 

You forgot to put any actual question in your question... I assume that you mean the title to be the question... ;)

You can use an extension and a delegate to make something that looks like a right-to-left evaluation. Actually it's still evaluated from left to right, but the code to the left is put in a delegate and only used based on the result of the condition on the right.

You can make an extension to the Action delegate with the name If.

public static class PostCondition {
   public static void If(this Action action, bool condition) {
      if (condition) action();
   }
}

Now you can make a delegate, cast it to Action, and call the If method on it:

((Action)(() => Console.WriteLine("Yes"))).If(true);

It get's a bit cleaner if you divide it into two statements:

Action writeYes = () => Console.WriteLine("Yes");
writeYes.If(true);

Still, it's not really an improvement over the regular if statement anyway...

Guffa
+2  A: 

No it doesn't check the MS C# lang spec or the ECMA334 spec (chapter 14)

SpaceghostAli
I don't know why this is being voted down. Everyone else is offering speculation and hearsay, this guy's bringing out the language specification. The answer is on page 28, only assignment and the conditional operator are right-associative.
A: 

I assume you're actually referring to operators that are right-associative, which are operators that act on the right side of the operators.

Most operators in C# are left-associative, except for:

  • The assignment operator (x = expression)
  • All unary operators ("-" , "!", "~"), including type casting (but that's not really an operator)

Technically, there's another one: the ternary operator (a ? b : c), but that's such a different beast that it hardly qualifies as a right-associative operator.

Philippe Leybaert