Add support implicit conversion between types

ilya-stromberg ilya-stromberg-2009 at yandex.ru
Fri Sep 6 03:33:05 PDT 2013


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.


More information about the Digitalmars-d mailing list