www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implicit conversion from a base/array type

reply "alexhairyman" <alexhairyman gmail.com> writes:
Is there a way to implicitly convert *FROM* a base type? I have 
an implicit conversion to a base type (float[2]) in a struct, but 
now I'd like to be able to implicitly convert from a base type 
(in this case a float[2]) to a struct.

Is this even allowed? Is it incorrect or unsafe? I'm still pretty 
new so there could be a major reason to not want to do this. I 
also did a lot of searching of the site, I hope I didn't miss 
something major, but I might have.

example :

struct Coord {...} // X,Y wrapper-like type, implicitly converts 
to/from float

void DoCoord(Coordinate c) {... do stuff...}

void main()
{
   DoCoord ([0.0f, 5.0f]); // Is it possible to set this up?
}
Feb 01 2014
next sibling parent reply "Martijn Pot" <martijnpot52 gmail.com> writes:
I tried something similar to (check first answer):
http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean

but I can't get it to work. But then again... I'm just starting 
with D.

It seems not to be supported:
http://forum.dlang.org/thread/teddgvbtmrxumffrhojh forum.dlang.org#post-qxqdksfoamarwjtpjcfq:40forum.dlang.org
Feb 01 2014
parent alexhairyman <alexhairyman gmail.com> writes:
On Sat, 01 Feb 2014 21:18:12 +0000, Martijn Pot wrote:

 I tried something similar to (check first answer):
 http://stackoverflow.com/questions/121162/what-does-the-explicit-
keyword-in-c-mean
 
 but I can't get it to work. But then again... I'm just starting with D.
 
 It seems not to be supported:
 http://forum.dlang.org/thread/teddgvbtmrxumffrhojh forum.dlang.org#post-
qxqdksfoamarwjtpjcfq:40forum.dlang.org Thanks, a little bit more work, but thanks for the link
Feb 01 2014
prev sibling parent reply "TheFlyingFiddle" <theflyingfiddle gmail.com> writes:
On Saturday, 1 February 2014 at 20:26:27 UTC, alexhairyman wrote:
 Is there a way to implicitly convert *FROM* a base type? I have 
 an implicit conversion to a base type (float[2]) in a struct, 
 but now I'd like to be able to implicitly convert from a base 
 type (in this case a float[2]) to a struct.

 Is this even allowed? Is it incorrect or unsafe? I'm still 
 pretty new so there could be a major reason to not want to do 
 this. I also did a lot of searching of the site, I hope I 
 didn't miss something major, but I might have.

 example :

 struct Coord {...} // X,Y wrapper-like type, implicitly 
 converts to/from float

 void DoCoord(Coordinate c) {... do stuff...}

 void main()
 {
   DoCoord ([0.0f, 5.0f]); // Is it possible to set this up?
 }
D currencly has no implicit casting operator overloading. It has been proposed before in http://wiki.dlang.org/DIP52 but i am not sure what the state of that is as of now.
Feb 01 2014
parent alexhairyman <alexhairyman gmail.com> writes:
On Sat, 01 Feb 2014 21:23:07 +0000, TheFlyingFiddle wrote:

 On Saturday, 1 February 2014 at 20:26:27 UTC, alexhairyman wrote:
 Is there a way to implicitly convert *FROM* a base type? I have an
 implicit conversion to a base type (float[2]) in a struct, but now I'd
 like to be able to implicitly convert from a base type (in this case a
 float[2]) to a struct.

 Is this even allowed? Is it incorrect or unsafe? I'm still pretty new
 so there could be a major reason to not want to do this. I also did a
 lot of searching of the site, I hope I didn't miss something major, but
 I might have.

 example :

 struct Coord {...} // X,Y wrapper-like type, implicitly converts
 to/from float

 void DoCoord(Coordinate c) {... do stuff...}

 void main()
 {
   DoCoord ([0.0f, 5.0f]); // Is it possible to set this up?
 }
D currencly has no implicit casting operator overloading. It has been proposed before in http://wiki.dlang.org/DIP52 but i am not sure what the state of that is as of now.
Hmmm, interesting
Feb 01 2014