www.digitalmars.com         C & C++   DMDScript  

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

reply Mike Chaten <mchaten gmail.com> writes:
--0016e6dee8e8f9f4d10492e97235
Content-Type: text/plain; charset=ISO-8859-1

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

--0016e6dee8e8f9f4d10492e97235
Content-Type: text/html; charset=ISO-8859-1

<p>In C++ it is possible to declare a class as follows<br>
class Foo {<br>
Foo(int x) { }<br>
}<br>
You can then use that constructor to implicitly convert int to Foo. E.g<br>
Foo x = 0; //equivalent to Foo(0)</p>
<p>Is there a way in D to do an implicit or explicit conversion from an
integral type to a class?</p>
<p>-Mike</p>

--0016e6dee8e8f9f4d10492e97235--
Oct 18 2010
next sibling parent "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
prev sibling next sibling parent Mike Chaten <mchaten gmail.com> writes:
--001636c5b994de6d210492e9ab36
Content-Type: text/plain; charset=ISO-8859-1

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

--001636c5b994de6d210492e9ab36 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <p>I was under the impression that alias this just was shorthand for<br> Class Foo {<br> int x;<br> alias x this;<br> }<br> Foo foo =3D=A0 new Foo<br> foo =3D 9; // foo.x =3D 9<br> Foo Foo =3D 9 // null.x =3D9;<br></p> <p>Also, for opCast, doesnt that only work for going from Foo to int and no= t the other way around?=A0 </p> <p>-Mike </p> <div class=3D"gmail_quote">On Oct 18, 2010 3:55 PM, &quot;Steven Schveighof= fer&quot; &lt;<a href=3D"mailto:schveiguy yahoo.com">schveiguy yahoo.com</a=
&gt; wrote:<br type=3D"attribution">&gt; On Mon, 18 Oct 2010 15:47:40 -040=

a>&gt; wrote:<br> &gt; <br>&gt;&gt; In C++ it is possible to declare a class as follows<br>&g= t;&gt; class Foo {<br>&gt;&gt; Foo(int x) { }<br>&gt;&gt; }<br>&gt;&gt; You= can then use that constructor to implicitly convert int to Foo. E.g<br> &gt;&gt; Foo x =3D 0; //equivalent to Foo(0)<br>&gt;&gt;<br>&gt;&gt; Is the= re a way in D to do an implicit or explicit conversion from an<br>&gt;&gt; = integral type to a class?<br>&gt; <br>&gt; explicit cast =3D=3D opCast<br>&= gt; implicit cast =3D=3D alias this<br> &gt; <br>&gt; Look up those two features in the docs.<br>&gt; <br>&gt; -Ste= ve<br></div> --001636c5b994de6d210492e9ab36--
Oct 18 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 next 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
prev sibling next sibling parent "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
prev sibling parent Mike Chaten <mchaten gmail.com> writes:
--00032555878efe9bce0492f91f47
Content-Type: text/plain; charset=ISO-8859-1

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

--00032555878efe9bce0492f91f47 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks for all your help!=A0<div><br></div><div>-Mike<br><div><br><div clas= s=3D"gmail_quote">On Mon, Oct 18, 2010 at 6:21 PM, Simen kjaeraas <span dir= =3D"ltr">&lt;<a href=3D"mailto:simen.kjaras gmail.com">simen.kjaras gmail.c= om</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div class=3D"im">Mike Chaten &lt;<a href= =3D"mailto:mchaten gmail.com" target=3D"_blank">mchaten gmail.com</a>&gt; w= rote:<br> <br> </div><div class=3D"im"><blockquote class=3D"gmail_quote" style=3D"margin:0= 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> I was under the impression that alias this just was shorthand for<br> Class Foo {<br> int x;<br> alias x this;<br> }<br> Foo foo =3D =A0new Foo<br> foo =3D 9; // foo.x =3D 9<br> Foo Foo =3D 9 // null.x =3D9;<br> </blockquote> <br></div> Not just. this would also work:<br> <br> int n =3D foo;<br> // void bar( int n ) {}<br> bar( foo );<div class=3D"im"><br> <br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Also, for opCast, doesnt that only work for going from Foo to int and not<b= r> the other way around?<br> </blockquote> <br></div> Indeed. For implicit casts to Foo, I don&#39;t know what, if anything, work= s.<br> <br> void baz( Foo f ) {}<br> baz( 3 ); // How?<br> <br> Likely, there is no such functionality in D, at least for the moment.<br> <br> -- <br><font color=3D"#888888"> Simen<br> </font></blockquote></div><br></div></div> --00032555878efe9bce0492f91f47--
Oct 19 2010