<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/4.2.2">
</HEAD>
<BODY>
Or template inference based on return type like<BR>
<BR>
T hello(T)()<BR>
{<BR>
static if (is(T ==)) ....<BR>
<BR>
}<BR>
<BR>
string v = hello();<BR>
<BR>
Il giorno mer, 18/07/2012 alle 17.38 +0100, Regan Heath ha scritto:
<BLOCKQUOTE TYPE=CITE>
<PRE>
On Tue, 17 Jul 2012 15:23:05 +0100, bearophile <<A HREF="mailto:bearophileHUGS@lycos.com">bearophileHUGS@lycos.com</A>>
wrote:
> Andrea Fontana:
>
>> class Known
>> {
>> void* data; // external data by c api
>> int type; // 0 for int, 1 for string, etc. ..
>> }
>>
>> How can I implement a method like this?
>>
>> Known known; // <-- suppose known.type == 1;
>> string s = known.value(); // <-- automatic
>
> To do this Known.value() needs to return different types according to
> the run-time value of Known.type. This is not possible in a statically
> typed language... You need to find other solutions.
Unless we had overload based on return type, right?
e.g.
class Known
{
string value()
{
if (type != 1)
throw..;
return cast(string)data;
}
int value()
{
if (type != 0)
throw ..;
return cast(int)data;
}
}
The compiler could produce the correct code/call for the line
string s = known.value();
then, but it's not a feature we're likely to see any time soon.
R
</PRE>
</BLOCKQUOTE>
<BR>
</BODY>
</HTML>