<div class="gmail_quote">On Fri, Apr 23, 2010 at 18:46, Robert Clipsham <span dir="ltr"><<a href="mailto:robert@octarineparrot.com">robert@octarineparrot.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">On 23/04/10 17:22, #ponce wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
In C++ implicit constructors and conversion operators allow a user-defined type to act quite like a builtin-type.<br>
<br>
struct half<br>
{<br>
half(float x);l<br>
inline operator float() const;<br>
}<br>
<br>
allows to write:<br>
<br>
half x = 1.f;<br>
float f = x;<br>
<br>
and this is especially useful for templates.<br>
I couldn't find a similar construct in D, is there any?<br>
</blockquote>
<br></div></div>
A combination of alias this and opAssign should work:<br>
----<br>
struct half<br>
{<br>
float f;<br>
alias f this;<br>
half opAssign(float fl)<br>
{<br>
f = fl;<br>
return this;<br>
}<br>
}<br>
<br>
void main()<br>
{<br>
half x;<br>
// For some reason I got a compilation error with half x = 1f;<br>
x = 1f;<br>
float f = x;<br>
}<br></blockquote><div><br>Could the compilation error comes from the compiler rewriting half f = 1f; into half f(1f); or somesuch and trying to call a constructor?<br>I'm tearing my hair due to errors/confusions between constructors and opCall for structs.<br>
<br>Anyway, in your example, adding <br><br>this(float f) { this.f = f;}<br><br>is enough to get half f = 1f; to compile.<br><br></div></div>