digitalmars.D.learn - Magic type return
- Andrea Fontana <nospam example.com> Jul 17 2012
- "Tobias Pankrath" <tobias pankrath.net> Jul 17 2012
- "bearophile" <bearophileHUGS lycos.com> Jul 17 2012
- Andrea Fontana <nospam example.com> Jul 17 2012
- Mirko Pilger <pilger cymotec.de> Jul 17 2012
- Philippe Sigaud <philippe.sigaud gmail.com> Jul 18 2012
- Andrea Fontana <nospam example.com> Jul 18 2012
- "Regan Heath" <regan netmail.co.nz> Jul 18 2012
- Andrea Fontana <nospam example.com> Jul 19 2012
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
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 =3D=3D 1;
string s =3D known.value(); // <-- automatic=20
I just know how to do this:
string s =3D know.value!string();=20
Jul 17 2012
On Tuesday, 17 July 2012 at 13:56:29 UTC, Andrea Fontana wrote: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 I just know how to do this: string s = know.value!string();
You can't. You could do string s; known.value(s); where void value(T)(ref T t);
Jul 17 2012
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. Bye, bearophile
Jul 17 2012
Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Better than nothing :) Hope in better template deduction... Il giorno mar, 17/07/2012 alle 16.22 +0200, Tobias Pankrath ha scritto:On Tuesday, 17 July 2012 at 13:56:29 UTC, Andrea Fontana wrote: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 =3D=3D 1; string s =3D known.value(); // <-- automatic I just know how to do this: string s =3D know.value!string();
You can't. You could do =20 string s; known.value(s); =20 where =20 void value(T)(ref T t);
Jul 17 2012
i'm not completely sure i understand your problem but i think you are looking for something like this: http://pocoproject.org/docs/Poco.DynamicAny.html maybe the c++ source code could be of some inspiration. this should be possible in d, too.
Jul 17 2012
--e0cb4efe326c496eae04c51bab9a Content-Type: text/plain; charset=UTF-8class 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 I just know how to do this: string s = know.value!string();
As bearophile said, you cannot change a value's type (which is a compile-time construct) with a runtime value, as is Known.type. 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. In your example, what happens if known.type != 1? 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. --e0cb4efe326c496eae04c51bab9a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <p>> class Known<br> > {<br> > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 void* data; // extern= al data by c api<br> > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 int type;=C2=A0 // 0 = for int, 1 for string, etc. ..<br> > }<br> ><br> > How can I implement a method like this?<br> ><br> > Known=C2=A0 known;=C2=A0 // <-- suppose known.type =3D=3D 1;<br> > string s=C2=A0 =3D known.value(); // <-- automatic <br> ><br> > I just know how to do this:<br> ><br> > string s =3D know.value!string(); </p> <p>As bearophile said, you cannot change a value's type (which is a com= pile-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 k= nowing it will be assigned to a string.<br> In your example, what happens if known.type !=3D 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> --e0cb4efe326c496eae04c51bab9a--
Jul 18 2012
Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Yes I did it using Variant and it works fine Il giorno mer, 18/07/2012 alle 16.42 +0200, Philippe Sigaud ha scritto: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 =3D=3D 1; string s =3D known.value(); // <-- automatic=20 I just know how to do this: string s =3D know.value!string();=20
As bearophile said, you cannot change a value's type (which is a compile-time construct) with a runtime value, as is Known.type.=20 =20 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. In your example, what happens if known.type !=3D 1?=20 =20 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. =20
Jul 18 2012
On Tue, 17 Jul 2012 15:23:05 +0100, bearophile <bearophileHUGS lycos.com> 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 -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Jul 18 2012
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Or template inference based on return type like
T hello(T)()
{
static if (is(T =3D=3D)) ....
}
string v =3D hello();
Il giorno mer, 18/07/2012 alle 17.38 +0100, Regan Heath ha scritto:
On Tue, 17 Jul 2012 15:23:05 +0100, bearophile <bearophileHUGS lycos.com>=
wrote:
=20
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 =3D=3D 1;
string s =3D 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?
=20
e.g.
=20
class Known
{
string value()
{
if (type !=3D 1)
throw..;
return cast(string)data;
}
=20
int value()
{
if (type !=3D 0)
throw ..;
return cast(int)data;
}
}
=20
The compiler could produce the correct code/call for the line
=20
string s =3D known.value();
=20
then, but it's not a feature we're likely to see any time soon.
=20
R
=20
Jul 19 2012









"Tobias Pankrath" <tobias pankrath.net> 