www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Replacement for C++ Style Implicit casts?

reply Mike Chaten <mchaten gmail.com> writes:
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
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
parent reply Mike Chaten <mchaten gmail.com> writes:
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
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
parent Mike Chaten <mchaten gmail.com> writes:
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 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 19 2010
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
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?

 -Mike
For 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
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
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