digitalmars.D - Feature Idea: Implicit Type conversions
- teqDruid (20/20) Jul 29 2004 It'd be cool if D could do implicit type conversions to/from any type.
- Jarrett Billingsley (3/3) Jul 29 2004 i use implicit conversions in C++ all the time, and i agree, they are
- Arcane Jill (16/22) Jul 30 2004 I wanted to let people do:
- Juanjo =?ISO-8859-15?Q?=C1lvarez?= (8/42) Jul 30 2004 Me too me too! In fact I didn't knew D didn't have implicit conversions ...
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
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
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
Arcane Jill wrote:In article <pan.2004.07.29.23.23.44.667148 teqdruid.com>, teqDruid says...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.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