Re: Anders Hejlsberg comment on immutable objects

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Martin Maat [EBL] (dummy_at_somewhere.nl)
Date: 02/09/04


Date: Tue, 10 Feb 2004 00:26:27 +0100


"Magnus Lidbom" <magnus_lidbom@hotmail.com> wrote in message
news:tgkf20lr223jo6pb3kh6cqktq8c51ufkbo@4ax.com...

> I wrote a post, with a short comparison of const and read only
> interfaces, not long ago that you may find enlightening:

>
http://groups.google.com/groups?q=g:thl1806205673d&dq=&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=bvhnig%24sd2t9%241%40ID-204195.news.uni-berlin.de&rnum=38

Okay, you compare using interfaces to the const keyword and conclude that
const is more concise and thus more powerful. It would be if it were the
same as using interfaces. The problem with const as I understand it is that
it will never be quite clear what exactly is to be considered const. The
implementor (or definor of the language if you will) has to choose a level
of protection ranging from just the object reference pointer (pretty
useless) to any dependencies one might access through the object. The latter
would be stronger but could turn out to be equally useless because it
ultimately would mean that the code querying the object should be denied
write access to anything, meaning it would not work.

I think the const concept just doesn't fit the object concept. Const is okay
for simple types but has no meaning to objects. In other words: it makes
sense for data but not for behavior.

It seems to me that if one says "it would be nice if we could apply const to
object arguments" one doesn't grasp the concequences.

I am not too confident myself here and the previous statement is not to say
you don't know what you are talking about (it sounds a bit like an insult),
my point is that I think once we apply const to objects, the possible
complexity is infinite and it would be impossible to implement (if you take
the protection serious anyway).

> Not really. He just pointed out the fact that you must distinguish the
> parameter from the object referenced by the parameter in the case of
> reference type parameters, and that the ref and out keywords doesn't
> help in managíng access to that object.

That is the issue, managing access to the object. I don't see how it could
be done because the compiler doesn't know what changes the object and what
doesn't. Like with the Sort method or retrieval of contained object
properties that may themselves expose write methods. The compiler could
never track that and sometimes it may not even make sense to do so. It would
be like asking the compiler to understand the semantics of your class and
then make judgements about what should be considered change and what should
not.

Some ten years ago the buz was fuzzy logic. It never took on. This reminds
me of fuzzy logic, I don't think I want it in C#.

Martin.



Relevant Pages

  • Re: Reference counting bug D6
    ... | a) never have interfaces as const parameters or b) use a cast like ... is not a bug, it is as it should be. ... method with a const parameter; therefore there is no reference to increment ...
    (borland.public.delphi.language.objectpascal)
  • Re: top-level const-ness inhibits override
    ... Actually, the shim needs to be virtual, but the implementation doesn't. ... I had less than two dozen functions total, and my interfaces were for ... virtual void get_Sum(double* const pdRetval) const override ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Anders Hejlsberg comment on immutable objects
    ... with a short comparison of const and read only ... you compare using interfaces to the const keyword and conclude that ... >same as using interfaces. ... Const is okay ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: abstract sealed & literal
    ... which can be invoked through a const handle, ... really be a part of the .NET CLR type system, with reflection support. ... interfaces (which would also be a very good thing although many of the ...
    (microsoft.public.dotnet.languages.vc)
  • Re: How to delete commas and spaces when not needed ... S&R way, or macro way?
    ... Just change the two Const assignments to the column letter and sheet name where your data is located at... ... Dim R As Range ... values show up in these three ways, the first one being okay, the ... need to get rid of comma and space _after_ the text). ...
    (microsoft.public.excel.programming)