www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - struct : implicit conversion for function call arguments

reply d coder <dlang.coder gmail.com> writes:
--0015174c3390826d3204d23d6f46
Content-Type: text/plain; charset=ISO-8859-1

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

--0015174c3390826d3204d23d6f46
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div><font face=3D"courier new, monospace">Greetings</font></div><div><font=
 face=3D"courier new, monospace"><br></font></div><div><font face=3D"courie=
r new, monospace">I observed that struct type function call arguments do no=
t implicitly convert even when I define a valid constructor for type conver=
sion. So DMD complains for line 15 in the code listed below saying:</font><=
/div>

<div><font face=3D"courier new, monospace"><div>test.d(15): Error: function=
 test.fiz (Bar _bar) is not callable using argument types (Foo)</div><div>t=
est.d(15): Error: cannot implicitly convert expression (foo) of type Foo to=
 Bar</div>

<div><br></div><div>Is this a bug, or is it intended behavior? If it is a b=
ug, can somebody please point me to the issue number filed on Bugzilla so t=
hat I can track it. I made some effort to search it myself but could find i=
t on Bugzilla.</div>

<div><br></div><div>And if it is intended behavior, I guess it is because o=
f some issues with template function matching. But I find explicit casting =
while calling functions a bit of a stretch.</div><div><br></div><div>Regard=
s</div>

<div>- Puneet</div><div><br></div></font></div><font face=3D"courier new, m=
onospace">struct Foo { } =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // 1<br>=A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// 2<br>struct Bar {=
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // 3<br>=A0 this (Foo _foo) { } =A0 =
=A0 =A0 =A0 =A0// 4<br>

=A0 void opAssign(Foo _foo) { } =A0// 5<br>} =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// 6<br>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0// 7<br>void fiz(Bar _bar) { } =A0 =A0 =A0 =A0 /=
/ 8<br>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// 9<=
br>void main() { =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// 10<br>

=A0 Foo foo; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // 11<br>=A0 // implic=
it conversion works // 12<br>=A0 Bar bar =3D foo; =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 // 13<br>=A0 // But not here =A0 =A0 =A0 =A0 =A0 =A0 =A0// 14<br>=A0 fi=
z(foo); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// 15<br>} =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0// 16</font><br>

<div><font face=3D"courier new, monospace"><br></font></div>

--0015174c3390826d3204d23d6f46--
Jan 01 2013
next sibling parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
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
Jan 01 2013
prev sibling next sibling parent d coder <dlang.coder gmail.com> writes:
--0015175cfca0e986ea04d23ed9a3
Content-Type: text/plain; charset=ISO-8859-1

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

addition to constructor path. Regards - Puneet --0015175cfca0e986ea04d23ed9a3 Content-Type: text/html; charset=ISO-8859-1 <br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> That&#39;s not how you define implicit conversions in D. It&#39;s a weird hack from C++.<br> <br></blockquote><div><br></div><div>Thanks for clarification. I knew about alias this, but I thought it was in addition to constructor path.</div><div><br></div><div>Regards</div><div>- Puneet</div></div> --0015175cfca0e986ea04d23ed9a3--
Jan 01 2013
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Jan 01 2013