views:

390

answers:

6

I want to create a ternary operator for a < b < c which is a < b && b < c. or any other option you can think of that a < b > c and so on... I am a fan of my own shortform and I have wanted to create that since I learned programming in high school.

How?

A: 

Have you tried searching for ternary operators in google to see if something already exists?

Mark Redman
Yes, the 2 hour (this time) search still showed me that a ? b : c appears to be the only ternary operation.
Scott S
+3  A: 

You can't do that. You can only implement existing operators, and there is no ternary .<.<. operator in C#.

Besides, such an operator would be ambiguous with existing comparing operators. For example, would the expression a == b == c == d mean ((a == b) == c) == d or (a == b == c) == d?

Guffa
Scott S
Guffa
Well, the idea could be expansive, similar to a function call with an arbitrary number of parameters. But it would also be based on a library that I would use to bypass standard equivalence tests.
Scott S
A: 

No. Not in C#. Language extension in this way is not available in any current version of C#, but Luca Bolognese pointed to using the compiler as a service and some functionality which could permit extending the language in such a way here: http://microsoftpdc.com/Sessions/FT11.

codekaizen
A: 

If you want to do that for the primitive datatypes, you're out of luck. C# doesn't support adding operators to those.

In your own datatypes it is possible to return a special datatype which stores the intermediate result and the comparisson result. However, I suggest you just stick to the c# language - if you really want the a < b < c operator style, switch to Python.

Koert
Would it be easy to integrate Python methods into C# funtion calls as a dual language program? (my experience with Python has been helping friends debug and it looked more like a scripting language)
Scott S
Python is a full-flegded programming language, and IronPython is an implementation of it that runs on the .NET platform and hence is interoperable with C#. See http://ironpython.net/
Håvard S
Interoperable - yes, but they still can't be mixed; you always only work in one language, so using the a<b<c idiom in c# is still out of the question
Koert
+5  A: 

Sorry, you cannot create your own operators in C#.

You could use extension methods to enable a fluent syntax like

bool f = b.IsBetween(a, c);

Or, if you were being extremely clever, you could do:

bool f = a.IsLessThan(b).IsLessThan(c);

doing so is tricky, but possible. (Hint: define a custom object that IsLessThan returns that tracks its bounds and understands how it is combined with other instances of the object. Essentially this is how LINQ-to-SQL works with regard to combining Where, Select, and so on.)

But you cannot define your own operator syntaxes in C#.

If you are interested in languages where you can define your own operators, you might consider looking into F#.

Eric Lippert
+5  A: 
Porges
That's pretty cool. So you can do `if (1.Chain() < i < 5)`
Igor Zevaka