views:

247

answers:

4

Possible Duplicate:
Is it possible to create a new operator in c#?

I love C#, but one thing I wish it had was the ability to define my own operators on classes, like A => B instead of having to do A.Implies(B). I think it would be really cool if you could assign an identifier of any length in a set like [+-*/&^|%$#@><]+ to a class method and then use it like an operator. Does anybody know if there's some sort of extension, or if it's even possible to make one, that does this?

A: 

In short, no. You would have to look at another language. You can overload many of the existing operators, or define any other methods (including extension methods which are very handy) - but you can't define custom operators in any variant of C# that I know of.

Marc Gravell
And there are restrictions on what you can do with the existing operators - e.g. if you define `==` you must define `!=`, etc. and the argument/return types are not totally open in all cases.
Daniel Earwicker
A: 

No, this is not possible, unless you make your own C#-like compiler.

You can only override some of the built-in operators, you can not create new ones, and the lambda operator (=>) cannot be overridden.

Lasse V. Karlsen
A: 

This is not built into the language. You are stuck with overloading the predefined operators that come with the language (overloadable operators).

If you like that style of debugging you might want to consider F#. It is a functional language that runs on top the .NET framework and gives you the ability to define any operator you want.

smaclell
280Z28
Removing the word sadly. I have been having too much fun with F# and ruby lately with their inline DSL magic.
smaclell
A: 

The list of operators that can be overloaded is here.

Operator overloading only lets you change the behavior of operators, not define new ones.

And although A => B as A.Implies(B) might sound cool, it will cause you problems later (in 6 months, when you're trying to figure out what your code is supposed to do).

Seth
By that argument, we should be using `a.Plus(b)` in case we forget what `+` means in arithmetic, or string concatenation. If you can decide on a meaning for a symbol in a domain, and then you use it a lot, it's *extremely* easy to remember what it means. If you're not going to use it a lot, it's not worth inventing the symbol. So in practise, this is not a problem.
Daniel Earwicker
@Earwicker: I don't buy it. `+` is universally understood. While `=>` may make all the sense in the world to you, if someone else comes along and has to maintain your code, they might not find it so easy to realize or remember what's going on.
David Brown
@David Brown: `+` means string concatenation in C#. It doesn't in VB.NET. So it is not universally understood in all its meanings. You are assuming that it is better to write code so an ill-informed person can read it slowly - consider that it may be better to write it so an informed person can read it fast. Right after C# 3 came out, most C# programmers didn't know what `=>` meant in a lambda. Were the C# team wrong to use that symbol? No, they expected the community to absorb the new meaning. Similarly, a library that invents its own symbols serves a community, and you can join it or not.
Daniel Earwicker
@Earwicker It is better to write code that is easy to read. Every time I've seen an operator overload used in code, it's always something weird like `businessObj1 % businessObj2`, or `~businessObj3`. It's no good to have to refer back to the manual twice on every line of code because there are operator overloads everywhere. On the other hand, I frequently switch between 4 languages, depending on what time of day it is, so I try real hard to keep things as consistent as possible. (And `+` *does* mean string concatenation in VB.Net).
Seth
Evidence of misuse does not rule out the existence of appropriate uses. Anyone suitably idiotic can abuse any technique: suppose you insist on named methods. An idiot could use the names `Method1`, `Method2`, etc. Meanwhile, check out the Irony parser library, an ideal example of operator overloading to create an "internal DSL" in C#, in this case resembling BNF: http://irony.codeplex.com/wikipage?title=expression%20grammar%20sample
Daniel Earwicker
Re: VB.NET, I was thinking of VB Script.
Daniel Earwicker
@Earwicker - I try not to think of VB script or VB.NET :P
Seth