www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Casts

reply bearophile <bearophileHUGS lycos.com> writes:
With reddit I have found a page that shows casts in C#:
http://rusek.org/stefan/default.aspx/2008/10/22/the-3-cast-operators-in-c/73/

The default cast of C# throws an exception if the dynamic cast can't be done.
Do you see this as safer than the current D1 cast?

The "as" of C# is similar to the current dynamic cast of D1.

The "is" operatotor of C# can be implemented with a cast plus test:
if (cast(Foo)o !is null) { ...
That sometimes I write:
if (IsInstance!(Foo)(o)) { ...

This syntax of C#
something = o as Foo ?? new Foo(0);

Becomes in D:
something = cast(Foo)o;
if (something is null) something = new Foo(0);

Because this shorter code requires two casts, it's slower:
something = cast(Foo)o is null ? new Foo(0) : cast(Foo)o;

I think in D may be useful to split the current cast() into two different
syntaxes, one similar the current dynamc cast, and the other that performs a
non-aliasing re-interepretation of the given bitpattern (the kind of conversion
unions are often used for in C). I find those two cases quite different, but I
confuse them in the current D1, so I think they may deserve two different
syntaxes. 

(This may look like an increase of the complexity of D, but when two (or more)
usages are conflated into a single syntax (or one common usage doesn't have a
hansy syntax) then the complexity of the language seems actually higher).

--------------

Sometimes you need to test a given object to many classes:

if (cast(Foo1)obj !is null) {
    ...
} else if (cast(Foo2)obj !is null) {
    ...
} else if (cast(Foo3)obj !is null) {
    ...
} else if (cast(Foo4)obj !is null) {
    ...
} else {
    ...
}

If such idioms are used often enough (in my code I don't use them often) then
something shorter and/or faster may be adopted.

Bye,
bearophile
Oct 22 2008
parent "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 22 Oct 2008 22:39:10 +0400, bearophile <bearophileHUGS lycos.com>  
wrote:

 With reddit I have found a page that shows casts in C#:
 http://rusek.org/stefan/default.aspx/2008/10/22/the-3-cast-operators-in-c/73/

 The default cast of C# throws an exception if the dynamic cast can't be  
 done. Do you see this as safer than the current D1 cast?

 The "as" of C# is similar to the current dynamic cast of D1.

 The "is" operatotor of C# can be implemented with a cast plus test:
 if (cast(Foo)o !is null) { ...
 That sometimes I write:
 if (IsInstance!(Foo)(o)) { ...

 This syntax of C#
 something = o as Foo ?? new Foo(0);

 Becomes in D:
 something = cast(Foo)o;
 if (something is null) something = new Foo(0);

 Because this shorter code requires two casts, it's slower:
 something = cast(Foo)o is null ? new Foo(0) : cast(Foo)o;

 I think in D may be useful to split the current cast() into two  
 different syntaxes, one similar the current dynamc cast, and the other  
 that performs a non-aliasing re-interepretation of the given bitpattern  
 (the kind of conversion unions are often used for in C). I find those  
 two cases quite different, but I confuse them in the current D1, so I  
 think they may deserve two different syntaxes.

 (This may look like an increase of the complexity of D, but when two (or  
 more) usages are conflated into a single syntax (or one common usage  
 doesn't have a hansy syntax) then the complexity of the language seems  
 actually higher).

 --------------

 Sometimes you need to test a given object to many classes:

 if (cast(Foo1)obj !is null) {
     ...
 } else if (cast(Foo2)obj !is null) {
     ...
 } else if (cast(Foo3)obj !is null) {
     ...
 } else if (cast(Foo4)obj !is null) {
     ...
 } else {
     ...
 }

 If such idioms are used often enough (in my code I don't use them often)  
 then something shorter and/or faster may be adopted.

 Bye,
 bearophile

You can use the following short-cut to avoid double casting: if (auto foo = cast(Foo)obj) { ... } else if (auto bar = cast(Bar)bar) { ... } else { ... }
Oct 22 2008