[OT] #define

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 22 09:54:51 PDT 2017


On Monday, 22 May 2017 at 13:52:35 UTC, Dukc wrote:
> On Monday, 22 May 2017 at 13:11:15 UTC, Andrew Edwards wrote:
>> Sorry if this is a stupid question but it eludes me. In the 
>> following, what is THING? What is SOME_THING?
>[...]
>
> I assume you know that the above part is c/c++ preprocessor, 
> which is not normally used at d?

Yes, I am aware.

> THING is nothing there. If you use it at code, I'm not sure how 
> it behaves. Either it does not compile at all, or it behaves as 
> nothing (think whitespace). If it's the latter, SOME_THING 
> would be a lone asterix. Can be used as operator both unarily 
> and binarily. #IFNDEF regonizes all preprocessor declarations, 
> empty or not.
>
> The closest D equivalent to #DEFINE is a string mixin. The 
> equivalent to above is:
> static if (!is(typeof(thing))) enum thing = "";
> static if (!is(typeof(someThing))) enum someThing = thing ~ " 
> *";
>[...]
>
> ...you have to explicitly mix the text in:
>
> @safe void main()
> {   import std.stdio;
>     mixin("writeln(5 " ~ someThing ~ " 6);"); //30
> }

That's really ugly... Hope I'll never have to resort to that. The 
specific idiom does have it's uses though, as in the case of 
self-important lookups.

> [...]
>
> -If the symbol is an empty declaration used only for #IFNDEFS, 
> version statements are a cood canditate for replacing it. 
> Perhaps that's what you're looking for?

Not exactly but I get the point. This list is golden, thanks for 
the assist.

Andrew



More information about the Digitalmars-d-learn mailing list