www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [proposal] How to overload opCast

reply Dusty <Dusty_member pathlink.com> writes:
Good day!
The one substantial drawback of D in comparition with C++ is that in D is
impossible to write more than one opCast per type. The reason is inability of
lincer to resolve different functions basing on respective return types.

But D - opposing to C++ - have "out" parameter modifier. So linker can easy
choose between proper functions:
# struct a {
#   void opCast(out int r);
#   void opCast(out double r);
#   void opCast(out char[] r);
# };

So I propose to add respective variants as valid choise when compiler resolve
code like 
# cast(char[]) a;

Dusty.
Aug 16 2005
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Dusty wrote:
 Good day!
 The one substantial drawback of D in comparition with C++ is that in D is
 impossible to write more than one opCast per type. The reason is inability of
 lincer to resolve different functions basing on respective return types.
 
 But D - opposing to C++ - have "out" parameter modifier. So linker can easy
 choose between proper functions:
 # struct a {
 #   void opCast(out int r);
 #   void opCast(out double r);
 #   void opCast(out char[] r);
 # };
 
 So I propose to add respective variants as valid choise when compiler resolve
 code like 
 # cast(char[]) a;

Its been proposed a dozen times before. And I for one still stand strongly in support of it. The fact that it keeps on coming back up proves that it bears consideration. -- Chris Sauls
Aug 16 2005
parent Shammah Chancellor <Shammah_member pathlink.com> writes:
In article <dds8rs$2n8u$1 digitaldaemon.com>, Chris Sauls says...
Dusty wrote:
 Good day!
 The one substantial drawback of D in comparition with C++ is that in D is
 impossible to write more than one opCast per type. The reason is inability of
 lincer to resolve different functions basing on respective return types.
 
 But D - opposing to C++ - have "out" parameter modifier. So linker can easy
 choose between proper functions:
 # struct a {
 #   void opCast(out int r);
 #   void opCast(out double r);
 #   void opCast(out char[] r);
 # };
 
 So I propose to add respective variants as valid choise when compiler resolve
 code like 
 # cast(char[]) a;

Its been proposed a dozen times before. And I for one still stand strongly in support of it. The fact that it keeps on coming back up proves that it bears consideration. -- Chris Sauls

I'm in favor as well, and was considering posting something exactly like this a few weeks ago! -Sha
Aug 16 2005
prev sibling next sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Dusty wrote:

 Good day!
 The one substantial drawback of D in comparition with C++ is that in D is
 impossible to write more than one opCast per type. The reason is inability of
 lincer to resolve different functions basing on respective return types.

Several proposals have been made for multiple cast operators. http://www.wikiservice.at/wiki4d/wiki.cgi?FeatureRequestList Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on on the 'group where everyone may benefit.
Aug 16 2005
prev sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Dusty" <Dusty_member pathlink.com> wrote in message 
news:dds783$2lv5$1 digitaldaemon.com...
 Good day!
 The one substantial drawback of D in comparition with C++ is that in D is
 impossible to write more than one opCast per type. The reason is inability 
 of
 lincer to resolve different functions basing on respective return types.

 But D - opposing to C++ - have "out" parameter modifier. So linker can 
 easy
 choose between proper functions:
 # struct a {
 #   void opCast(out int r);
 #   void opCast(out double r);
 #   void opCast(out char[] r);
 # };

 So I propose to add respective variants as valid choise when compiler 
 resolve
 code like
 # cast(char[]) a;

 Dusty.

I'm curious what you were coding that requires overloaded opCast (and, actually, what required an opCast at all). Note "cast(char[])a" in D would normally be code up as "a.toString()". Also D doesn't let you add to the set of implicit conversions the compiler tries to apply. All overloading has to happen on explicit casting expressions cast(Foo)bar. D is much less free-wheeling than C++ when it comes to implicit and explicit conversions and overloading.
Aug 16 2005