<p>> class Known<br>
> {<br>
> void* data; // external data by c api<br>
> int type; // 0 for int, 1 for string, etc. ..<br>
> }<br>
><br>
> How can I implement a method like this?<br>
><br>
> Known known; // <-- suppose known.type == 1;<br>
> string s = known.value(); // <-- automatic <br>
><br>
> I just know how to do this:<br>
><br>
> string s = know.value!string(); </p>
<p>As bearophile said, you cannot change a value's type (which is a compile-time construct) with a runtime value, as is Known.type. </p>
<p>Second point, in D, the rhs is fully evaluated before being assigned to the lhs, I think. So, known.value() must evaluate to *something*, without knowing it will be assigned to a string.<br>
In your example, what happens if known.type != 1? </p>
<p>You can use Phobos Variant, (or Algebraic if the range of types you plan to use is known beforehand). Then, you should test typeid before using it.</p>