Thread overview | |||||
---|---|---|---|---|---|
|
July 04, 2013 Print out the name of a type | ||||
---|---|---|---|---|
| ||||
Suppose I've got a function that is conditional on a type: void foo(T)(/* input vars */) { ... } How could I print out using writeln the name of that type? So that if I call foo!double(...); ... I would see: Today, Michael, I'm going to be a double! ... whereas if I give it, foo!uint(...); I'd see Today, Michael, I'm going to be a uint! and so on. Ideally this should also work for complex data types such as structs, classes, etc. |
July 04, 2013 Re: Print out the name of a type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joseph Rushton Wakeling | On Thursday, 4 July 2013 at 14:46:36 UTC, Joseph Rushton Wakeling wrote: > ... Each has own disadvantages, chose one ;) T.stringof // simple & reliable, won't work for function aliases __traits(identifier, T) // only symbols std.traits.fullyQualifiedName!T // issues with templated types, includes module/package into name |
July 04, 2013 Re: Print out the name of a type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joseph Rushton Wakeling | On Thursday, 4 July 2013 at 14:51:21 UTC, Artur Skawina wrote:
> On 07/04/13 16:46, Joseph Rushton Wakeling wrote:
>> Suppose I've got a function that is conditional on a type:
>>
>> void foo(T)(/* input vars */) { ... }
>>
>> How could I print out using writeln the name of that type? So that if I call
>>
>> foo!double(...);
>>
>> ... I would see:
>>
>> Today, Michael, I'm going to be a double!
>
> writeln("Today, Michael, I'm going to be a "~T.stringof~"!")
>
> artur
What he said. Or if you don't even have T, use "typeof(t).stringof".
This also works in pragma msg:
pragma(msg, T.stringof);
|
Copyright © 1999-2021 by the D Language Foundation