www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature Idea: Implicit Type conversions

reply teqDruid <me teqdruid.com> writes:
It'd be cool if D could do implicit type conversions to/from any type. 
For instance:
int[] foo(int[] param);
Array!(int) a ...;
a = foo(a);
Given that Array has some sort of int[] toArray(), and a constructor that
accepts just an int[].  Of course, this need not be limited to arrays- any
type could work, although one of the obvious applications is making a
String class that is implicitly convertable to/from char[], so one can do:
String s = "hello"; /* Instead of: */ String s = new String("hello");
Without String being built into the language.

The compiler just has to search for constructors that accept what one is
trying to convert from, and/or a method (the name starting with "to") that
returns what one is trying to convert to.  Or, instead of a method that
starts with "to", something like:
int[] convert(int[] dummy);
So it just looks for a method that accepts (and returns) what one is
trying to convert to, since one cannot method overload based on return
types.

Thoughts?
Jul 29 2004
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
i use implicit conversions in C++ all the time, and i agree, they are
useful.  i'm not sure why they aren't implemented in D.  perhaps it has
something to do with the funny cast(type)value syntax ;)
Jul 29 2004
prev sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <pan.2004.07.29.23.23.44.667148 teqdruid.com>, teqDruid says...
It'd be cool if D could do implicit type conversions to/from any type. 
although one of the obvious applications is making a
String class that is implicitly convertable to/from char[], so one can do:
String s = "hello"; /* Instead of: */ String s = new String("hello");
Without String being built into the language.
I wanted to let people do: Currently, they have to do: which isn't anywhere near as nice. So I agree with you. I'd like to see implicit conversion happening either: (a) from type T, if the class has a constructor taking a single parameter of type T. (Of course, this may require adding an "explicit" keyword to prevent unwanted accidental conversions). (b) to type T, if the class can cast to that type. (Of course, this requires that opCast() be made useful - i.e. capable of being overloaded for more than one type. Again, an "explicit" keyword might be helpful to prevent accidental conversions).
Thoughts?
I'm with you on that one. Jill
Jul 30 2004
parent Juanjo =?ISO-8859-15?Q?=C1lvarez?= <juanjuxNO SPAMyahoo.es> writes:
Arcane Jill wrote:

 In article <pan.2004.07.29.23.23.44.667148 teqdruid.com>, teqDruid says...
It'd be cool if D could do implicit type conversions to/from any type.
although one of the obvious applications is making a
String class that is implicitly convertable to/from char[], so one can do:
String s = "hello"; /* Instead of: */ String s = new String("hello");
Without String being built into the language.
I wanted to let people do: Currently, they have to do: which isn't anywhere near as nice. So I agree with you. I'd like to see implicit conversion happening either: (a) from type T, if the class has a constructor taking a single parameter of type T. (Of course, this may require adding an "explicit" keyword to prevent unwanted accidental conversions). (b) to type T, if the class can cast to that type. (Of course, this requires that opCast() be made useful - i.e. capable of being overloaded for more than one type. Again, an "explicit" keyword might be helpful to prevent accidental conversions).
Thoughts?
I'm with you on that one. Jill
Me too me too! In fact I didn't knew D didn't have implicit conversions (I sould be writing some D code...) and this have shocked me. I use implicit conversions _all the time_ when I code in C++ (mostly char*<->SomeLibString<->MySomeLibStringSon) but also for a ^ ^ `------------------------' lot other things.
Jul 30 2004