digitalmars.D.learn - get classname in static member
- number (36/36) Mar 28 2018 Is there a way to get the classname without specifying the class
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (17/24) Mar 28 2018 While this is not available in static functions (since there's no
- number (4/31) Mar 28 2018 Thank you for the explanations. I somehow forgot about typeof
- bauss (7/9) Mar 28 2018 stringof will return a string equivalent to the expression or
Is there a way to get the classname without specifying the class in the first place as required by classinfo and fullyQualifiedName? extracting it from __FUNCTION__ wouldn't work outside a function, i.e. for a classfield, and 'this' doesn't work in static members. It's just about simple debugging like 'writeln(<classname>, ": message: ..")' ..and learning. And could somebody explain to me why 'typeid(this).stringof' is returning 'typeid(this)'? I'm using dmd v2.078.3 ``` import std.stdio; void main() { class C { static void foo() { writeln(__FUNCTION__); // test.main.C.foo writeln(C.classinfo); // test.main.C import std.traits; // writeln(fullyQualifiedName!C); // test.main.C } void fuu() { writeln(this); // test.main.C writeln(typeid(this)); // test.main.C writeln(typeid(this).stringof); // typeid(this) writeln(this.classinfo); // test.main.C } } C.foo(); writeln(); C c = new C(); c.fuu; } ```
Mar 28 2018
On Wednesday, 28 March 2018 at 09:44:35 UTC, number wrote:Is there a way to get the classname without specifying the class in the first place as required by classinfo and fullyQualifiedName? extracting it from __FUNCTION__ wouldn't work outside a function, i.e. for a classfield, and 'this' doesn't work in static members.While this is not available in static functions (since there's no instance to point to), typeof(this) is. So to get the class name you'd generally use typeof(this).stringof.And could somebody explain to me why 'typeid(this).stringof' is returning 'typeid(this)'?Because that's what you're asking for. :p typeid returns the run-time (also called dynamic) type of a class instance. stringof is a compile-time construct that resolves to a compile-time constant string representation of its argument. If that argument is a type, it gives the name of the type. If it's an expression (such as 1+2, foo(), or typeid(this)), it gives a string representation of the expression. For typeid(this), that's "typeid(this)". If you want to know the dynamic type of a class reference at run-time, such as in fuu(), you'd need to write writeln(typeid(this)). -- Simen
Mar 28 2018
On Wednesday, 28 March 2018 at 10:10:19 UTC, Simen Kjærås wrote:So to get the class name you'd generally use typeof(this).stringof. ...On Wednesday, 28 March 2018 at 10:12:34 UTC, bauss wrote:And could somebody explain to me why 'typeid(this).stringof' is returning 'typeid(this)'?Because that's what you're asking for. :p typeid returns the run-time (also called dynamic) type of a class instance. stringof is a compile-time construct that resolves to a compile-time constant string representation of its argument. If that argument is a type, it gives the name of the type. If it's an expression (such as 1+2, foo(), or typeid(this)), it gives a string representation of the expression. For typeid(this), that's "typeid(this)". If you want to know the dynamic type of a class reference at run-time, such as in fuu(), you'd need to write writeln(typeid(this)). -- SimenOn Wednesday, 28 March 2018 at 09:44:35 UTC, number wrote:Thank you for the explanations. I somehow forgot about typeof though i must have seen it a lot in 'Ali Cehreli's Book.And could somebody explain to me why 'typeid(this).stringof' is returning 'typeid(this)'?stringof will return a string equivalent to the expression or symbol's representation, not its definition. Meaning: (212 + 221).stringof == "212 + 221" (new Foo).stringof == "new Foo" etc.
Mar 28 2018
On Wednesday, 28 March 2018 at 09:44:35 UTC, number wrote:And could somebody explain to me why 'typeid(this).stringof' is returning 'typeid(this)'?stringof will return a string equivalent to the expression or symbol's representation, not its definition. Meaning: (212 + 221).stringof == "212 + 221" (new Foo).stringof == "new Foo" etc.
Mar 28 2018