Support implicit conversion between types

Namespace rswhite4 at googlemail.com
Wed Sep 4 12:44:16 PDT 2013


On Wednesday, 4 September 2013 at 19:36:08 UTC, ilya-stromberg 
wrote:
> 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?

What's about:

void bar(int i) {
     bar(Foo(i));
}

?


More information about the Digitalmars-d-learn mailing list