Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 22, 2008 Casts | ||||
---|---|---|---|---|
| ||||
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 |
October 22, 2008 Re: Casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 {
...
}
|
February 06, 2018 Re: Casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | On Wednesday, 22 October 2008 at 18:43:15 UTC, Denis Koroskin wrote: > On Wed, 22 Oct 2008 22:39:10 +0400, bearophile <bearophileHUGS@lycos.com> wrote: > >> [...] > > 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 { > ... > } doesn't work with extern(C++) classes see https://forum.dlang.org/post/nolylatfyjktnvweyxlu@forum.dlang.org |
February 06, 2018 Re: Casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to timotheecour | On 2/6/18 4:16 PM, timotheecour wrote:
> On Wednesday, 22 October 2008 at 18:43:15 UTC, Denis Koroskin wrote:
>> On Wed, 22 Oct 2008 22:39:10 +0400, bearophile <bearophileHUGS@lycos.com> wrote:
>>
>>> [...]
>>
>> 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 {
>> ...
>> }
>
> doesn't work with extern(C++) classes see https://forum.dlang.org/post/nolylatfyjktnvweyxlu@forum.dlang.org
Thimotheecour, I'm not sure how you find these threads. This one is almost 10 years old! Back then, C++ class integration wasn't even a twinkling in Walter's eye ;)
-Steve
|
February 06, 2018 Re: Casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Tuesday, 6 February 2018 at 21:35:11 UTC, Steven Schveighoffer wrote:
> On 2/6/18 4:16 PM, timotheecour wrote:
>> On Wednesday, 22 October 2008 at 18:43:15 UTC, Denis Koroskin wrote:
>>> On Wed, 22 Oct 2008 22:39:10 +0400, bearophile <bearophileHUGS@lycos.com> wrote:
>>>
>>>> [...]
>>>
>>> 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 {
>>> ...
>>> }
>>
>> doesn't work with extern(C++) classes see https://forum.dlang.org/post/nolylatfyjktnvweyxlu@forum.dlang.org
>
> Thimotheecour, I'm not sure how you find these threads. This one is almost 10 years old! Back then, C++ class integration wasn't even a twinkling in Walter's eye ;)
>
> -Steve
Lol, I did the same thing once...I think I may have clicked a link on an old thread, and when I went back to the "page view" it must have gone back to the "page view" where the original thread was listed. I saw an interesting thread an responded and then someone let me know it was 7 years old...I had no idea!!!
|
February 06, 2018 Re: Casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Marler | On Tuesday, 6 February 2018 at 21:41:14 UTC, Jonathan Marler wrote:
>
> Lol, I did the same thing once...I think I may have clicked a link on an old thread, and when I went back to the "page view" it must have gone back to the "page view" where the original thread was listed. I saw an interesting thread an responded and then someone let me know it was 7 years old...I had no idea!!!
Link didn't work for me either and didn't realize it until I checked the date. Was able to use the internet wayback machine to get a snap from like 2008.
|
February 06, 2018 Re: Casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | well even old threads are useful to update when there's new information ; because they show up in search results so good to keep answers up to date (and provide link to newer info)
On Tue, Feb 6, 2018 at 1:44 PM, jmh530 via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> On Tuesday, 6 February 2018 at 21:41:14 UTC, Jonathan Marler wrote:
>>
>>
>> Lol, I did the same thing once...I think I may have clicked a link on an old thread, and when I went back to the "page view" it must have gone back to the "page view" where the original thread was listed. I saw an interesting thread an responded and then someone let me know it was 7 years old...I had no idea!!!
>
>
> Link didn't work for me either and didn't realize it until I checked the date. Was able to use the internet wayback machine to get a snap from like 2008.
|
Copyright © 1999-2021 by the D Language Foundation