struct : implicit conversion for function call arguments

Peter Alexander peter.alexander.au at gmail.com
Tue Jan 1 09:40:40 PST 2013


On Tuesday, 1 January 2013 at 17:27:10 UTC, d coder wrote:
> I observed that struct type function call arguments do not 
> implicitly
> convert even when I define a valid constructor for type 
> conversion.

That's not how you define implicit conversions in D. It's a weird 
hack from C++.

In D, you use alias this for implicit type conversions.
http://dlang.org/class.html#AliasThis

If you change Foo to this:

struct Foo
{
   Bar asBar() { return Bar(this); }
   alias asBar this;
}

Your code will work.

Note however that alias this has a few bugs.
http://d.puremagic.com/issues/buglist.cgi?query_format=advanced&short_desc=alias%20this&bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&short_desc_type=allwordssubstr


More information about the Digitalmars-d mailing list