Re: Anders Hejlsberg comment on immutable objects

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Daniel O'Connell [C# MVP] (onyxkirx_at_--NOSPAM--comcast.net)
Date: 02/12/04


Date: Thu, 12 Feb 2004 11:04:21 -0600


<snip>
> > >> The type system, quite correctly, consider an ArrayList to be an
> > >> Object. No cast, implicit or otherwise, is employed to pass an
> > >> ArrayList as an Object or to invoke Object members on an ArrayList
> > >> instance. I'd consider casting to a base class obfuscation since a
> > >> cast is a clear indication that a dangerous conversion is performed
> > >> and this is simply not the case for such a convection.
> > >>
> > >> Do you really write code like this?
> > >>
> > >> int i = 1;
> > >> Console.WriteLine((object)i);
> > >> Type intType = ((Object)i).GetType();
> > >>
> > >> Do you really consider code like that to improve maintainability and
> > >> legibility?
> > >>
> > >That isn't what I meant. What you are illustrating there is a cast, not
a
> > >conversion. My problem is with conversions, such as if ArrayList
defined
> an
> > >implicit conversion to an array. To be clear, a conversion creates a
new
> > >object(usually) of a different type
> >
> > Not neccessarily. This is probably where our disagreement comes from.
> > The C# language spec should clear things up:
> >
> > http://www.jaggersoft.com/csharp_standard/13.1.4.htm
> >
> > As per the spec, what I'm doing is applying a cast that is completely
> > unneccessary since subclass to baseclass is an implicit reference
> > conversion. The same would be true had I cast to IComparable instead.
> > There is no new object created. See the bottom note at the page above.
> > In fact the casts above should generate no IL what so ever.
>
> The spec here is using the word conversion (in conjunction with reference,
> as in "reference conversion") literaly.
> Magnus, the "conversions" you are talking about are simply "casts" in
> Daniel's mind.
> As he says, casts do not change the value of the object casted.
> The conversions Daniel refers to are those, defined with a conversion
> operator in C#:
>
> public static implicit operator T1 (T2 v)
> {
> ...somehow convert the object v from type T2 to T1, pottentially
> creating a new object...
> }
>
> Excuse me if that was clear to both of you, it might very well have been.
In
> that case, please ignore this message.
> Just trying to help...
>
This is pretty much waht I mean, conversion operators.
> On a sideline, I have to disagree with Daniel on the usefullness of
> conversions.
> Conversions (and especially implicit ones) are invaluable asset in C#, one
> of the things that makes it far more expressive than Java.
> I agree there is dark side to them in the sense that for someone might not
> be obvious what is going on, but still, I think the benefits are well
worth
> it.
I dislike implicit conversion operators, mainly because they are unapparent.
I would have no problem with explicit conversion operators if they didn't
use the cast operator, instead opting for different syntax. The cast and
implicit conversions make it to hard to determine exactly what is going on.
I also have issues with the way operators work, being static they are
unreliable, I simply don't care for them.
>
> >
> >
> > <snip>
> >
> > Regards /Magnus Lidbom
> >
>
>



Relevant Pages

  • Re: Anders Hejlsberg comment on immutable objects
    ... > This is pretty much waht I mean, conversion operators. ... > use the cast operator, ... Again, it is almost always apparent what you want to do in Java, but to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: malloc and free
    ... >> an implicit conversion, as well as those that result from a cast ... A cast is something generated by a cast operator. ... Unless the type name specifies a void type, ... A cast that specifies no conversion has no effect on the type ...
    (comp.lang.c)
  • Re: allocate memory statically
    ... The cast is necessary. ... in C because an *implicit* conversion is invoked by default. ... C++ does *not* specify any such implicit conversion ... but modern quality ANSI/ISO C99 compliant compiler ...
    (comp.lang.c)
  • Re: Replies to Upcasting vs downcasting.
    ... I take this example to be a conversion cast, although I can see how it ... could be considered a downcast. ... and "widening conversion" to encompass both reference conversions and non-reference conversions. ... Java always requires a cast for a narrowing conversion. ...
    (comp.lang.java.programmer)
  • Re: Terminology : casting/conversion
    ... Is this an "explicit" cast? ... conversion even if the type of the expression is the ... Here are p & p2 are pointers but the compiler "implicitly" converts ... this parse tree is almost unconnected to the ...
    (comp.lang.c)