Add support implicit conversion between types

Benjamin Thaut code at benjamin-thaut.de
Fri Sep 6 03:50:55 PDT 2013


Am 06.09.2013 12:33, schrieb ilya-stromberg:
> 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
>
>      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.

I also made almost the same suggestion some time ago. I would support a 
feature like this.

-- 
Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list