Twiddling my thumbs while I wait for the compiler to finish

I seem to be in some sort of state where if I ever touch this one file I trigger a complete rebuild of all our source. 

I’m in the middle of taking some of our core types and doing the dual job of making them

  1. Immutable objects

  2. Good const citizens

I’m trying ot architect this work for maximum clarity and simplicity.  I’m also trying to get it in a form that would be easy to port to C# and would end up with code that I wouldn’t want to change once ported over.

It’s easy to create immutable objects in C#.  The readonly modifier does a great job of that for me.  However, the lack of an ability to declare something const is extremely frustrating.  About the only thing I can do to ensure the same behavior is to provide a deep clone on every single one of my types and then send a clone over to any callers.  This is extremely frustrating and error prone.

I understand that in C++ it’s purely an affectation and anyone can get around the constness of an object trivially.  However, I would love it if our runtime added support to runtime checking of ‘const.’  The only reasons I can think of for us not to do this are:

  1. Perf overhead of checking that it’s ok to call a specific method on a const instance of a type

  2. Needing to go through the BCL to update all methods with teh correct const information.

Both would cost a lot in terms of time necessary to do it.  However, I see the benefit as being so high.  Unfortunately, the longer we go without adding this, the more unlikely it will ever be that we’ll ever be able to add it.

Comments (6)

  1. Cyrus,

    there has been a discussion with raymond chen just some time ago regarding this… ( )

    eric ( ) and stan ( ) also posted stuff regarding this.

    but…. i still liked to see ‘const’ in the runtime… 🙂


    thomas woelfer

  2. const is also more familiar. readonly is… weird to me. const is familiar, I know it, I know what it does, what the semantics of it are, how to use it, when, why. readonly? Then you start the inevitable stream of questions beginning with "what’s the difference?"

  3. YES!!! I’ve never gotten an acceptable answer as to why there is no const… but I’m glad there are other people around who feel the same way I do.

    I talked about this a while ago on my blog here…

    It is the one major complaint I have left about C# & .NET

  4. Shawn says:

    One problem is that the CLR has to support multiple languages. If you add a CLR const feature, and go mark the BCL classes and methods with the modifier, you’ll be left in a situation where only languages that support the concept of const will work with the CLS. So in addition to modifying C#, you’d have to modify VB.Net, and all the other languages that compile to the CLS specification