C# – Purity of methods in C#, evaluation and custom compiler


Have you ever been frustrated by Visual Studio not re-evaluating certain watch expressions when stepping through code with the debugger?

I have and does anyone here care about pure methods? Methods with no side-effects? There's so many great things about C# that I love, but I can't write pure functions, that is, static methods with no side-effects.

What if you were to build your own C# compiler where you could write something like this.

function int func(readonly SomeRefType a, int x, int y) { return /*...*/; }

Not only is the above a free function, alas, I don't call it a method, the function is assured to not have any side-effects. The C# keyword readonly could here be used to indicate just that, and provide a contract for pure functions. These kind of functions can always be evulated, without causing side effects. This being a way for Visual Studio to always evaulate my functions in the watch, despite the faulty assumpation that all method calls and user operators have side effects. A method where all the parameters are copy by value can never have side effects, yet, Visual Studio fails to recognize this.

I love C++ for what you can do at compile-time and I miss these things in C#, I think C# is dumbing down on the user a bit and basically not allowing certain expressiveness, hurting programmers. Many things which actually relate what you can do at compile time, I'd like to actually see more meta programs which are programs run by the compiler to compiler your original program.

e.g. While C# has booleans and don't allow things like if (var a = obj as MyRefType), it doesn't generate the approriate code. I did some digging around and noticed how C# fails to generate approriate IL for branchless conditionals, for example x > y ? 1 : 0, there's an IL instruction for just that which the C# compiler dosen't use.

Would you want, or be instrested in an open-source .NET compiler? Which looks like C# but is something entierly different, more expressive, and more flexible, and totally whaack, in terms what you can do with it?

Best Solution

Not really. If I want different language options I've already got:

  • F# for a functional bent
  • Boo for a DSL helper with custom compilation stages

The chances of a "design by committee" (or even "design by single amateur language designer") language ending up as well thought out as C# are pretty slim, IMO.

Would it be nice to be able to express a few more things? Absolutely.

Is it rather handy having hundreds of thousands of people who understand the same language, built-in Visual Studio integration from the people who really know it, etc? Absolutely!

For me, "looks like C# but is something entirely different" sounds like a problem, not a solution.