tags:

views:

163

answers:

5

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?

+3  A: 

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.

Jon Skeet
Why default to pessimism? F# is not what I had in mind and Boo just looks weird, then again, I don't like Python. The fact is, I beleve that C# has mistakenly so introduced things and prohibited constructs which are vital to some aspects of programming.
John Leidegren
Where did user-defined deconstructors for structs go? Why is there even a boolean type? In practice, any non-zero value is a true constant, the boolean type generally serves no purpose. Why can't I put a decleration statement in an if statement? These things change what you can do with a language.
John Leidegren
This is what you get with C++, some people argue that that's too much and maybe I've just reached the point were I think C# is to limited and that's why I wanna build my own flavour of my favorite languages that targets the .NET platform.
John Leidegren
I don't "default to pessimism" - but you asked a question, and my honest answer is one that you don't like. Sure, go ahead and build your own flavour - but I doubt that you'll come up with anything that *I* would personally pick over C#.
Jon Skeet
If one of your ideas is to get rid of strongly typed Boolean expressions, that's just another indication that we think very differently. I'm really glad that C# differentiates between "true" and "not zero". If you don't, that's fine - but don't expect the world to unanimously agree with you.
Jon Skeet
Can you give me an example of where strongly typed Boolean expressions makes a huge difference? I've always found them more irratating than meaingful.
John Leidegren
The fact that I don't need to worry about "if (x=5)" being a typo is the first point. But more importantly, it's about expressing what you want to express. You say you want a *more* expressive language, but then you talk about removing the ability to express Booleans! (Continuted)
Jon Skeet
What would Predicate<T> look like in your language? Would you make it return an integer instead? That's obfuscating the meaning of the predicate - it logically gives a yes/no answer, not a 1/0 answer.
Jon Skeet
None of this is essential, and typos well, that just sad (it happens but disregard that fact). A Boolean expression boils down to not zero for anything that is true. Ergo, there is no Boolean type, just integers. Despite the fact that it might look counterintuitive, a Boolean type is redundant.
John Leidegren
Depending on what you mean by expressiveness and what I mean, it serves as a good thing or a bad thing. I wan't less hardware abstraction and thus the ineffcent and less transparent handling of a Boolean type is really not something that intrigues me.
John Leidegren
So you're happy to give up the expressiveness of "this isn't an integer, it's just a true or false value" *and* go back to the bad old days of incorrect code being valid... I think we have different goals, hence my original positioning: I'd have no interest in such a language.
Jon Skeet
I wouldn't call it expressiveness becuase it gets in the way. I guess I should go back to just programming C++, in a sense I think that's what I intend... I'm more an more leaning towards the idea that you don't make mistakes and when you do, well that's not the language's fault.
John Leidegren
Think of it like this. You don't blame the English language for your spelling mistakes. A spelling mistake is not enough to mess up your sentence but it's subtale and it can be confusing. Let's call that a bug. And then ask yourself why does programming have to be any different?
John Leidegren
The answer is obviously becuase a formal language isn't ambiguous. And it doesn't have to be like that, but if it was, it wouldn't be any different that wirting.
John Leidegren
I've *never* found it to get in the way - but I *have* found it to make code clearer. When something declares that it returns an int, that should mean it's a useful number. When something declares that it returns a bool, it just means true or false. It affords more meaning. That's expressive IMO.
Jon Skeet
IMO, it's a *good* thing for a programming language to make it harder to make mistakes. If you enjoy shooting yourself in the foot with no warning, that's fine - but please don't try to persuade me that it's something *I* want to be doing. And yes, it sounds like you'd be happier with C++.
Jon Skeet
A: 

Why bother with writing a different compiler? You can achieve at least some of what you describe here by, e.g., writing a PostSharp weaver to alter the method (umm, function) at compile-time.

Dmitri Nesteruk
A: 

Just follow this simple convention: If it's not void, don't touch the parameters.

Thomasz
A: 

If you really want a mix of purity but you can still go with imperative aspect of OOP, go with F#. It's from Microsoft and it'll be productized and integrated into VS 2010.

If you really want a real pure functional programming that you can be sure that you have pure functions in your programming constructs and clearly differentiate side effects including IOs, Exceptions, you can go with Haskell.

For starter, you can download GHC, an open source Haskell compiler from Simon Peyton Jones, a researcher from Microsoft. You can also download Visual Haskell, a plugin for VS 2005 that functions as IDE for Haskell.

For more info:

eriawan
+1  A: 

Implementing a language these days is a lot more than implementing a compiler. I'm personally addicted to Resharper, and even the more limited forms of syntax highlighting, hinting, auto-completion and in-editor compilation offered by plain Visual Studio is pretty sophisticated and helpful. All that UI stuff would have to be reimplemented as well.

A good case study in this area is the Spec# language. Rather than throwing more features into C#, Microsoft has instead turned it into a library that provides a way of decorating your code and then performing static checking on it, after the normal compilation step. So it is "bolted on" to whatever language you are already using.

This would probably be the most practical way to add more sophisticated verification to .NET languages.

Also, note that even though the language compiler generates IL with apparently redundant instructions, you aren't seeing what the JIT does when it produces more optimised native code.

Daniel Earwicker
yeah, that's the thing about contemporary developmnt, good tools are hard to come by.
John Leidegren