Support implicit conversion between types

ilya-stromberg ilya-stromberg-2009 at yandex.ru
Wed Sep 4 12:36:06 PDT 2013


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)
}


D can't implicitly convert type "int" to type "Foo", but 
constructor "Foo(int)" exists. Explicit conversion works fine.
What should I do to support this convertion implicitly?


More information about the Digitalmars-d-learn mailing list