digitalmars.D - Casts
- bearophile (33/33) Oct 22 2008 With reddit I have found a page that shows casts in C#:
- Denis Koroskin (10/53) Oct 22 2008 You can use the following short-cut to avoid double casting:
- timotheecour (4/15) Feb 06 2018 doesn't work with extern(C++) classes see
- Steven Schveighoffer (5/23) Feb 06 2018 Thimotheecour, I'm not sure how you find these threads. This one is
- Jonathan Marler (7/31) Feb 06 2018 Lol, I did the same thing once...I think I may have clicked a
- jmh530 (5/11) Feb 06 2018 Link didn't work for me either and didn't realize it until I
- Timothee Cour (5/16) Feb 06 2018 well even old threads are useful to update when there's new
http://rusek.org/stefan/default.aspx/2008/10/22/the-3-cast-operators-in-c/73/ Do you see this as safer than the current D1 cast? if (cast(Foo)o !is null) { ... That sometimes I write: if (IsInstance!(Foo)(o)) { ... 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
On Wed, 22 Oct 2008 22:39:10 +0400, bearophile <bearophileHUGS lycos.com> wrote:http://rusek.org/stefan/default.aspx/2008/10/22/the-3-cast-operators-in-c/73/ done. Do you see this as safer than the current D1 cast? if (cast(Foo)o !is null) { ... That sometimes I write: if (IsInstance!(Foo)(o)) { ... 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, bearophileYou 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
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:doesn't work with extern(C++) classes see https://forum.dlang.org/post/nolylatfyjktnvweyxlu forum.dlang.org[...]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 { ... }
Feb 06 2018
On 2/6/18 4:16 PM, timotheecour wrote:On Wednesday, 22 October 2008 at 18:43:15 UTC, Denis Koroskin wrote: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 ;) -SteveOn Wed, 22 Oct 2008 22:39:10 +0400, bearophile <bearophileHUGS lycos.com> wrote:doesn't work with extern(C++) classes see https://forum.dlang.org/post/nolylatfyjktnvweyxlu forum.dlang.org[...]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 { ... }
Feb 06 2018
On Tuesday, 6 February 2018 at 21:35:11 UTC, Steven Schveighoffer wrote:On 2/6/18 4:16 PM, timotheecour 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!!!On Wednesday, 22 October 2008 at 18:43:15 UTC, Denis Koroskin wrote: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 ;) -SteveOn Wed, 22 Oct 2008 22:39:10 +0400, bearophile <bearophileHUGS lycos.com> wrote:doesn't work with extern(C++) classes see https://forum.dlang.org/post/nolylatfyjktnvweyxlu forum.dlang.org[...]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 { ... }
Feb 06 2018
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.
Feb 06 2018
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.
Feb 06 2018