digitalmars.D.learn - Replacement for C++ Style Implicit casts?
- Mike Chaten (9/9) Oct 18 2010 In C++ it is possible to declare a class as follows
- Steven Schveighoffer (5/13) Oct 18 2010 explicit cast == opCast
- Mike Chaten (12/26) Oct 18 2010 I was under the impression that alias this just was shorthand for
- Simen kjaeraas (11/21) Oct 18 2010 Not just. this would also work:
- Mike Chaten (3/26) Oct 19 2010 Thanks for all your help!
- Denis Koroskin (9/18) Oct 18 2010 For structs the following works:
- bearophile (17/26) Oct 18 2010 Do you mean something like this?
In C++ it is possible to declare a class as follows class Foo { Foo(int x) { } } You can then use that constructor to implicitly convert int to Foo. E.g Foo x = 0; //equivalent to Foo(0) Is there a way in D to do an implicit or explicit conversion from an integral type to a class? -Mike
Oct 18 2010
On Mon, 18 Oct 2010 15:47:40 -0400, Mike Chaten <mchaten gmail.com> wrote:In C++ it is possible to declare a class as follows class Foo { Foo(int x) { } } You can then use that constructor to implicitly convert int to Foo. E.g Foo x = 0; //equivalent to Foo(0) Is there a way in D to do an implicit or explicit conversion from an integral type to a class?explicit cast == opCast implicit cast == alias this Look up those two features in the docs. -Steve
Oct 18 2010
I was under the impression that alias this just was shorthand for Class Foo { int x; alias x this; } Foo foo = new Foo foo = 9; // foo.x = 9 Foo Foo = 9 // null.x =9; Also, for opCast, doesnt that only work for going from Foo to int and not the other way around? -Mike On Oct 18, 2010 3:55 PM, "Steven Schveighoffer" <schveiguy yahoo.com> wrote:On Mon, 18 Oct 2010 15:47:40 -0400, Mike Chaten <mchaten gmail.com> wrote:In C++ it is possible to declare a class as follows class Foo { Foo(int x) { } } You can then use that constructor to implicitly convert int to Foo. E.g Foo x = 0; //equivalent to Foo(0) Is there a way in D to do an implicit or explicit conversion from an integral type to a class?explicit cast == opCast implicit cast == alias this Look up those two features in the docs. -Steve
Oct 18 2010
Mike Chaten <mchaten gmail.com> wrote:I was under the impression that alias this just was shorthand for Class Foo { int x; alias x this; } Foo foo = new Foo foo = 9; // foo.x = 9 Foo Foo = 9 // null.x =9;Not just. this would also work: int n = foo; // void bar( int n ) {} bar( foo );Also, for opCast, doesnt that only work for going from Foo to int and not the other way around?Indeed. For implicit casts to Foo, I don't know what, if anything, works. void baz( Foo f ) {} baz( 3 ); // How? Likely, there is no such functionality in D, at least for the moment. -- Simen
Oct 18 2010
Thanks for all your help! -Mike On Mon, Oct 18, 2010 at 6:21 PM, Simen kjaeraas <simen.kjaras gmail.com>wrote:Mike Chaten <mchaten gmail.com> wrote: I was under the impression that alias this just was shorthand forClass Foo { int x; alias x this; } Foo foo = new Foo foo = 9; // foo.x = 9 Foo Foo = 9 // null.x =9;Not just. this would also work: int n = foo; // void bar( int n ) {} bar( foo ); Also, for opCast, doesnt that only work for going from Foo to int and notthe other way around?Indeed. For implicit casts to Foo, I don't know what, if anything, works. void baz( Foo f ) {} baz( 3 ); // How? Likely, there is no such functionality in D, at least for the moment. -- Simen
Oct 19 2010
On Mon, 18 Oct 2010 23:47:40 +0400, Mike Chaten <mchaten gmail.com> wrote:In C++ it is possible to declare a class as follows class Foo { Foo(int x) { } } You can then use that constructor to implicitly convert int to Foo. E.g Foo x = 0; //equivalent to Foo(0) Is there a way in D to do an implicit or explicit conversion from an integral type to a class? -MikeFor structs the following works: struct Foo { this(int x) { ... } } Foo x = 0; Classes are allocated on heap and as such there is no way to achieve the same.
Oct 18 2010
Mike Chaten:In C++ it is possible to declare a class as follows class Foo { Foo(int x) { } } You can then use that constructor to implicitly convert int to Foo. E.g Foo x = 0; //equivalent to Foo(0) Is there a way in D to do an implicit or explicit conversion from an integral type to a class?Do you mean something like this? class Foo { int x; static Foo opCall(int x_) { auto f = new Foo; f.x = x_; return f; } } void main() { Foo f = Foo(5); assert(f.x == 5); } (With structs it's simpler) Bye, bearophile
Oct 18 2010