digitalmars.D - typeid() woes
- Andrej Mitrovic <andrej.mitrovich gmail.com> Aug 08 2010
- Andrej Mitrovic <andrej.mitrovich gmail.com> Aug 08 2010
- bearophile <bearophileHUGS lycos.com> Aug 08 2010
- Adam Ruppe <destructionator gmail.com> Aug 08 2010
- Andrej Mitrovic <andrej.mitrovich gmail.com> Aug 08 2010
test.d:
class Contact
{
}
class Friend : Contact
{
void reminder()
{
writeln("friend.reminder");
}
}
unittest
{
Contact c = new Friend; // c has type Contact but really refers to a Friend
writeln(typeid(c));
c.reminder();
}
This will not compile, as expected, since a Contact type doesn't have the
reminder method.
But if I comment out the c.reminder() line, writeln will return "test.Friend".
Why is that?
Aug 08 2010
--0014853d01301bd30b048d570fce Content-Type: text/plain; charset=ISO-8859-1 I need to be a little patient. :) On the next page it explains the static vs. dynamic type, so I guess typeid() always returns the dynamic type. I wonder if there's a way to get the static type, is there an equivalent function like typeid() for such a case? On Sun, Aug 8, 2010 at 11:57 PM, Andrej Mitrovic <andrej.mitrovich gmail.comwrote:
test.d: class Contact { } class Friend : Contact { void reminder() { writeln("friend.reminder"); } } unittest { Contact c = new Friend; // c has type Contact but really refers to a Friend writeln(typeid(c)); c.reminder(); } This will not compile, as expected, since a Contact type doesn't have the reminder method. But if I comment out the c.reminder() line, writeln will return "test.Friend". Why is that?
--0014853d01301bd30b048d570fce Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I need to be a little patient. :) On the next page it explains the static v= s. dynamic type, so I guess typeid() always returns the dynamic type. I won= der if there's a way to get the static type, is there an equivalent fun= ction like typeid() for such a case?<br> <br><div class=3D"gmail_quote">On Sun, Aug 8, 2010 at 11:57 PM, Andrej Mitr= ovic <span dir=3D"ltr"><<a href=3D"mailto:andrej.mitrovich gmail.com">an= drej.mitrovich gmail.com</a>></span> wrote:<br><blockquote class=3D"gmai= l_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204= , 204, 204); padding-left: 1ex;"> test.d:<br> <br> class Contact<br> {<br> }<br> <br> class Friend : Contact<br> {<br> =A0 =A0void reminder()<br> =A0 =A0{<br> =A0 =A0 =A0 =A0writeln("friend.reminder");<br> =A0 =A0}<br> }<br> <br> unittest<br> {<br> =A0 =A0Contact c =3D new Friend; // c has type Contact but really refers t= o a Friend<br> <br> =A0 =A0writeln(typeid(c));<br> =A0 =A0c.reminder();<br> }<br> <br> This will not compile, as expected, since a Contact type doesn't have t= he reminder method.<br> <br> But if I comment out the c.reminder() line, writeln will return "test.= Friend". Why is that?<br> </blockquote></div><br> --0014853d01301bd30b048d570fce--
Aug 08 2010
Andrej Mitrovic:I wonder if there's a way to get the static type, is there an equivalent function like typeid() for such a case?
Try typeof(). Bye, bearophile
Aug 08 2010
On 8/8/10, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:I wonder if there's a way to get the static type, is there an equivalent function like typeid() for such a case?
Try this: typeid(typeof( X )); typeof() returns the static type of the variable. Then, you can get the typeid of that returned static type. Here's a sample program: ==== import std.stdio; class Something { } class SomethingElse : Something {} void main() { Something o = new SomethingElse; writeln(typeid(typeof(o))); } ==== Prints: test6.Something
Aug 08 2010
--e0cb4e887ee96ef98d048d5760c9 Content-Type: text/plain; charset=ISO-8859-1 Thanks, that works. On Mon, Aug 9, 2010 at 12:21 AM, Adam Ruppe <destructionator gmail.com>wrote:On 8/8/10, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:I wonder if there's a way to get the static type, is there an equivalent function like typeid() for such a case?
Try this: typeid(typeof( X )); typeof() returns the static type of the variable. Then, you can get the typeid of that returned static type. Here's a sample program: ==== import std.stdio; class Something { } class SomethingElse : Something {} void main() { Something o = new SomethingElse; writeln(typeid(typeof(o))); } ==== Prints: test6.Something
--e0cb4e887ee96ef98d048d5760c9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Thanks, that works.<br><br><div class=3D"gmail_quote">On Mon, Aug 9, 2010 a= t 12:21 AM, Adam Ruppe <span dir=3D"ltr"><<a href=3D"mailto:destructiona= tor gmail.com">destructionator gmail.com</a>></span> wrote:<br><blockquo= te class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1= px solid rgb(204, 204, 204); padding-left: 1ex;"> <div class=3D"im">On 8/8/10, Andrej Mitrovic <<a href=3D"mailto:andrej.m= itrovich gmail.com">andrej.mitrovich gmail.com</a>> wrote:<br> > I wonder if there's a way to get the static type, is there an equi= valent<br> > function like typeid() for such a case?<br> <br> </div>Try this:<br> <br> =A0 =A0 =A0typeid(typeof( X ));<br> <br> typeof() returns the static type of the variable. Then, you can get<br> the typeid of that returned static type.<br> <br> Here's a sample program:<br> =3D=3D=3D=3D<br> import std.stdio;<br> class Something { }<br> class SomethingElse : Something {}<br> <br> void main() {<br> =A0 =A0 =A0 =A0Something o =3D new SomethingElse;<br> =A0 =A0 =A0 =A0writeln(typeid(typeof(o)));<br> }<br> =3D=3D=3D=3D<br> Prints: test6.Something<br> </blockquote></div><br> --e0cb4e887ee96ef98d048d5760c9--
Aug 08 2010









bearophile <bearophileHUGS lycos.com> 