Add support implicit conversion between types
Flamaros
flamaros.xavier at gmail.com
Fri Sep 6 06:25:35 PDT 2013
On Friday, 6 September 2013 at 10:33:07 UTC, ilya-stromberg wrote:
> Do you have any plans to support implicit conversion between
> types?
>
> I have some code like this:
>
> struct Foo
> {
> this(int i)
> {
> //do something useful
> }
> }
>
> void bar(Foo f)
> {
> //do something else
> }
>
> void main()
> {
> Foo f = 5;//works
>
> bar(f);//works
>
> bar(Foo(5));//works
I think that the good way, to do
> bar(5);//Error: function app.bar (Foo f) is not callable using
> argument types (int)
> }
>
> So, D can't use constructor to convert "int" to "Foo"
> implicitly.
> Can we add "implicit" keyword to allow do this:
>
> struct Foo
> {
> implicit this(int i)
> {
> //do something useful
> }
> }
>
> C++ allows this, but have "explicit" keyword.
> C# doesn't allow this, but have operator overloading for both
> implicit and explicit cases.
It's difficult to never forget the "explicit" keyword in c++, and
this little mistake can make you loose a lot of time.
I prefer to only have explicit conversions with cast or
constructors calls, that all make the code easier to understand.
More information about the Digitalmars-d
mailing list