Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 01, 2013 struct : implicit conversion for function call arguments | ||||
---|---|---|---|---|
| ||||
Attachments:
| Greetings I observed that struct type function call arguments do not implicitly convert even when I define a valid constructor for type conversion. So DMD complains for line 15 in the code listed below saying: test.d(15): Error: function test.fiz (Bar _bar) is not callable using argument types (Foo) test.d(15): Error: cannot implicitly convert expression (foo) of type Foo to Bar Is this a bug, or is it intended behavior? If it is a bug, can somebody please point me to the issue number filed on Bugzilla so that I can track it. I made some effort to search it myself but could find it on Bugzilla. And if it is intended behavior, I guess it is because of some issues with template function matching. But I find explicit casting while calling functions a bit of a stretch. Regards - Puneet struct Foo { } // 1 // 2 struct Bar { // 3 this (Foo _foo) { } // 4 void opAssign(Foo _foo) { } // 5 } // 6 // 7 void fiz(Bar _bar) { } // 8 // 9 void main() { // 10 Foo foo; // 11 // implicit conversion works // 12 Bar bar = foo; // 13 // But not here // 14 fiz(foo); // 15 } // 16 |
January 01, 2013 Re: struct : implicit conversion for function call arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to d coder | 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 |
January 01, 2013 Re: struct : implicit conversion for function call arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Alexander Attachments:
| > That's not how you define implicit conversions in D. It's a weird hack from C++.
>
>
Thanks for clarification. I knew about alias this, but I thought it was in addition to constructor path.
Regards
- Puneet
|
January 02, 2013 Re: struct : implicit conversion for function call arguments | ||||
---|---|---|---|---|
| ||||
On Wednesday, January 02, 2013 00:37:32 d coder wrote:
> > That's not how you define implicit conversions in D. It's a weird hack from C++.
>
> Thanks for clarification. I knew about alias this, but I thought it was in addition to constructor path.
Nope. It's the _only_ way to define implicit conversions in D. In general, D requires explicit conversions in order to avoid all of the problems that implicit conversions cause in C++ (e.g. C++ will do up to 3 implicit conversions when passing an argument to a function; that gets particularly nasty when combined with function overloading; you can quickly have no idea what's actually being called without stepping through the code).
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation