Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 20, 2007 implicit casts | ||||
---|---|---|---|---|
| ||||
there should be some way to define implicit casts either to or from user defined types. this would greatly reduce the amount of code since functions would no longer have to be overloaded for as many times. ex: a fraction class does not need to overload opAdd since integers can be implictly converted to fractions. |
July 20, 2007 Re: implicit casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ender KaShae | Reply to Ender, > there should be some way to define implicit casts either to or from > user defined types. this would greatly reduce the amount of code > since functions would no longer have to be overloaded for as many > times. ex: a fraction class does not need to overload opAdd since > integers can be implictly converted to fractions. > this would also simplify some things like what is described here: http://andersnoras.com/blogs/anoras/archive/2007/07/09/behind-the-scenes-of-the-planning-dsl.aspx things like this would be really slick |class EventComponent |{ | class Attend | { | Attend AreRequired(){ ... return this; } | Attend AreOptional(){ ... return this; } | | EventComponent opImplicitCast(){return super;} // this | } | | static EventComponent opImplicitCast(Attendants f){return f.super;} // or this | | Attendants{char[][] names...) | { | return new Attend(names); | } |} | |EventComponent e = new EventComponent(); |e.Attendants("joe", "jane").AreRequired.Attendants("bob", "bill").AreOptional; |
July 21, 2007 Re: implicit casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ender KaShae | Ender KaShae wrote:
> there should be some way to define implicit casts either to or from user defined types. this would greatly reduce the amount of code since functions would no longer have to be overloaded for as many times. ex: a fraction class does not need to overload opAdd since integers can be implictly converted to fractions.
C++ has the copy constructor. Perhaps it should be something along those lines. Although the C++ copy constructor is fraught with problems because you never know when a conversion may occur.
-Joel
|
July 21, 2007 Re: implicit casts | ||||
---|---|---|---|---|
| ||||
Posted in reply to janderson | janderson wrote:
> Ender KaShae wrote:
>> there should be some way to define implicit casts either to or from user defined types. this would greatly reduce the amount of code since functions would no longer have to be overloaded for as many times. ex: a fraction class does not need to overload opAdd since integers can be implictly converted to fractions.
>
> C++ has the copy constructor. Perhaps it should be something along those lines. Although the C++ copy constructor is fraught with problems because you never know when a conversion may occur.
C++ _copy_ constructors are used for copying, not converting. Other constructors in C++ are declared "explicit" to disallow the compiler from using them for implicit conversions, i.e., they'll only be used if explicitly requested by code.
-- James
|
Copyright © 1999-2021 by the D Language Foundation