www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to "stringnify"?

reply rempas <rempas tutanota.com> writes:
Is there a way to "stringnify" in Dlang? In C we would do 
something like the following:

`#define STRINGIFY(x) #x`

What's the equivalent in D?
Oct 10 2021
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Sunday, 10 October 2021 at 08:28:30 UTC, rempas wrote:
 Is there a way to "stringnify" in Dlang? In C we would do 
 something like the following:

 `#define STRINGIFY(x) #x`

 What's the equivalent in D?
For simple stuff you have .stringof
Oct 10 2021
parent reply rempas <rempas tutanota.com> writes:
On Sunday, 10 October 2021 at 08:48:21 UTC, Imperatorn wrote:
 For simple stuff you have .stringof
Thanks!
Oct 10 2021
parent bauss <jj_1337 live.dk> writes:
On Sunday, 10 October 2021 at 09:51:27 UTC, rempas wrote:
 On Sunday, 10 October 2021 at 08:48:21 UTC, Imperatorn wrote:
 For simple stuff you have .stringof
Thanks!
Just remember .stringof is not "literal" as in there are certain expression that won't return exactly what you're trying to "stringify" ex. aliases etc. will actually return what they're an alias of etc. there are many other exceptions too. In general it will return just what you write, but not in all cases.
Oct 10 2021
prev sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Sunday, 10 October 2021 at 08:28:30 UTC, rempas wrote:
 Is there a way to "stringnify" in Dlang? In C we would do 
 something like the following:

 `#define STRINGIFY(x) #x`

 What's the equivalent in D?
That's probably depends on what you are trying to achieve. If you want to write code-like string that looks like a code you can use token strings https://dlang.org/spec/lex.html#token_strings ```d string code = q{ float sqr(float x) { return x * x; } }; ``` For other symbols there is .stringof property (beware, it's behavior is not part of the spec) https://dlang.org/spec/property.html#stringof ```d class MyClass() { static this() { registerSmth(MyClass.stringof); // pass "MyClass" string } } ``` And there is stuff for metaprogramming such like this in std.traits https://dlang.org/phobos/std_traits.html#ParameterIdentifierTuple
Oct 10 2021
parent rempas <rempas tutanota.com> writes:
On Sunday, 10 October 2021 at 08:54:55 UTC, evilrat wrote:
 That's probably depends on what you are trying to achieve.

 If you want to write code-like string that looks like a code 
 you can use token strings

 https://dlang.org/spec/lex.html#token_strings

 ```d
 string code = q{
   float sqr(float x) { return x * x; }
 };
 ```

 For other symbols there is .stringof property (beware, it's 
 behavior is not part of the spec)

 https://dlang.org/spec/property.html#stringof

 ```d
 class MyClass()
 {
   static this() {
     registerSmth(MyClass.stringof); // pass "MyClass" string
   }
 }
 ```

 And there is stuff for metaprogramming such like this in 
 std.traits

 https://dlang.org/phobos/std_traits.html#ParameterIdentifierTuple
".stringof" will probably do the job! If not I will check "token_strings". Thanks a lot!
Oct 10 2021