www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is there a way to get the identifier (call it name) of what is being

reply someone <someone somewhere.com> writes:
I know the answer for this will be a resoundingly no, but, anyway 
...

I have, more-or-less, the following function:

```d
public void debugwriteln(typeValue)(
    const dstring lstrTag,
    const typeValue lstrValue
    ) {

    writeln(console.colorizeYellow(r"debugging"d), r" → "d,
       lstrTag,
       r"=["d,
       lstrValue,
       r"]"d
       );

} /// console.colorizeYellow() does the obvious thing on the 
terminal
```

... which I oftenly use as following to highlight what I am doing 
at any given time:

```d
void main() {

    ...

    debugwriteln(r"lobjExchanges.count"d, lobjExchanges.count);

    foreach (typeExchange lobjExchange; lobjExchanges) {

       debugwriteln(r"lobjExchange.toString()"d, 
lobjExchange.toString());

    ...

    }

}
```

... with typical output as following:

debugging → lobjExchanges.count=[2]
debugging → lobjExchange.toString()=[NYSE (New York Stock 
Exchange)   New York, USA on EST]
debugging → lobjExchange.toString()=[NASDAQ (National Association 
of Securities Dealers Automated Quotations)   New York, USA on 
EST]

Every time I use it, of course, I have to double-type the tag and 
the value. I wonder if there's some mechanism to just type the 
following:

```d
debugwriteln(lobjExchanges.count);
```

... and some way to get the identifier of what's being passed, he.
Jul 18 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 19 July 2021 at 00:07:25 UTC, someone wrote:

 Every time I use it, of course, I have to double-type the tag 
 and the value. I wonder if there's some mechanism to just type 
 the following:

 ```d
 debugwriteln(lobjExchanges.count);
 ```

 ... and some way to get the identifier of what's being passed, 
 he.
The closest you can get is to use a string mixin: ```d enum debugWriteln(string expression) = `writeln(q"(` ~ expression ~ `)", " = ", ` ~ expression ~ `);`; // Usage: mixin(debugWriteln!"lobjExchanges.count"); ```
Jul 18 2021
parent reply someone <someone somewhere.com> writes:
On Monday, 19 July 2021 at 00:52:39 UTC, Paul Backus wrote:

 The closest you can get is to use a string mixin:

 ```d
 enum debugWriteln(string expression) =
     `writeln(q"(` ~ expression ~ `)", " = ", ` ~ expression ~ 
 `);`;

 // Usage:
 mixin(debugWriteln!"lobjExchanges.count");
 ```
clever :)
Jul 18 2021
parent reply SealabJaster <sealabjaster gmail.com> writes:
On Monday, 19 July 2021 at 01:26:25 UTC, someone wrote:
 ...
Not 100% sure this works in all cases, but this version doesn't need the caller to make use of a mixin: https://run.dlang.io/is/3jLxLz
Jul 18 2021
next sibling parent reply someone <someone somewhere.com> writes:
On Monday, 19 July 2021 at 01:45:27 UTC, SealabJaster wrote:
 On Monday, 19 July 2021 at 01:26:25 UTC, someone wrote:
 ...
Not 100% sure this works in all cases, but this version doesn't need the caller to make use of a mixin: https://run.dlang.io/is/3jLxLz
http://dlang.org/traits.html ... huh, this is huge, I see *a lot* of useful things there :) !
Jul 18 2021
parent reply SealabJaster <sealabjaster gmail.com> writes:
On Monday, 19 July 2021 at 01:56:21 UTC, someone wrote:
 ...
Prepare yourself, as you may become a metaprogramming junkie in the near future. Throw in `static if`[0] `static foreach`[1] `string mixin()`[2] `CTFE`[3] and make some crazy, random, arcane mess of fun spaghetti. I've wrote two blogs about metaprogramming in D so far, to give you a bit of inspiration >:3 : 1. Writing text templates with embedded D code that is compiled at compile-time[4] 2. Writing a basic JSON serialiser[5] [0] https://dlang.org/spec/version.html#staticif [1] https://dlang.org/spec/version.html#staticforeach [2] https://dlang.org/articles/mixin.html [3] https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe [4] https://bradley.chatha.dev/dlang-compile-time-text-templates [5] https://bradley.chatha.dev/BlogPost/JsonSerialiser/0-serialise-basic-d-types-dlang-tutorial-metaprogramming
Jul 18 2021
parent reply SealabJaster <sealabjaster gmail.com> writes:
On Monday, 19 July 2021 at 03:51:02 UTC, SealabJaster wrote:
 ...
There's also Ali Cehreli's book, which is excellent. e.g. here's the section on UDAs: http://ddili.org/ders/d.en/uda.html
Jul 18 2021
parent someone <someone somewhere.com> writes:
On Monday, 19 July 2021 at 03:52:51 UTC, SealabJaster wrote:
 On Monday, 19 July 2021 at 03:51:02 UTC, SealabJaster wrote:
 ...
There's also Ali Cehreli's book, which is excellent.
I stepped into D alongside Andrei and Ali's books. Ali's one I was aware it existed but I didn't start to read it until I stepped into D. Andrei's one I have it (in dead-tree-media format) since a few years back (this book and a few things over there on the net were what sparked my interest in D to begin with) but didn't get the time to start messing with D until a few months ago. To me, there are two excellent books for totally different reasons: - Andrei's one is elegant, has style and finesse all over the place, is superbly organized more-or-less as I expect a programming language book has to be (or what I used to expect until I read Ali's one), it is always at my desk, and even when I am not coding I open it randomly and read a couple of pages while having coffee etc. I now it is outdated for a lot of things (2010), I would love to see a second edition of this book, really. - Ali's one is minimalist, pragmatic all the way-round, now that I learned he's a self-taught programmer I understand why this book is what it is (the foreword by Andrei explains this better than my words); at first it seemed to me the book was something like a huge collection of small articles not caring much about structure/organization/classification/whatever, at second glance I thought: this is the way we, self-taught programmers learn a new language: we didn't read huge section/chapters depicting the fundamentals and, then, only then, we did go to next big ticket, no, eg: we didn't learned everything under the sun about strings before we started to write our hello world's, we revisited strings further along our learning-curve the moment we needed something else, the book is a snapshot of a developer's learning curve: functions, more functions, even more functions, things like that.
 e.g. here's the section on UDAs: 
 http://ddili.org/ders/d.en/uda.html
But I didn't explore the more esoteric things yet; (I was aware that UDAs existed, I did glanced at them on Ali's book, but never made the connection with something akin this issue) the only thing that I read top-to-bottom up to this point was Philippe Sigaud's templates in D (attempting to solve some issue I did have back then), and there are a lot of things that I quite not fully-understand yet.
Jul 19 2021
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 19 July 2021 at 01:45:27 UTC, SealabJaster wrote:
 On Monday, 19 July 2021 at 01:26:25 UTC, someone wrote:
 ...
Not 100% sure this works in all cases, but this version doesn't need the caller to make use of a mixin: https://run.dlang.io/is/3jLxLz
If you only need to print variables, not arbitrary expressions, then this approach is much nicer. However, the mixin version is the only one that will work for things like `debugWriteln!"x + y"`.
Jul 18 2021
next sibling parent SealabJaster <sealabjaster gmail.com> writes:
On Monday, 19 July 2021 at 03:05:24 UTC, Paul Backus wrote:
 ...
Yea, that's true.
Jul 18 2021
prev sibling parent someone <someone somewhere.com> writes:
On Monday, 19 July 2021 at 03:05:24 UTC, Paul Backus wrote:
 On Monday, 19 July 2021 at 01:45:27 UTC, SealabJaster wrote:
 On Monday, 19 July 2021 at 01:26:25 UTC, someone wrote:
 ...
Not 100% sure this works in all cases, but this version doesn't need the caller to make use of a mixin: https://run.dlang.io/is/3jLxLz
If you only need to print variables, not arbitrary expressions, then this approach is much nicer.
Yes, just variables/properties.
 However, the mixin version is the only one that will work for 
 things like `debugWriteln!"x + y"`.
In this case a temp variable and I'm done.
Jul 18 2021