Jump to page: 1 2
Thread overview
is there a way to get the identifier (call it name) of what is being passed to a function ?
Jul 19, 2021
someone
Jul 19, 2021
Paul Backus
Jul 19, 2021
someone
Jul 19, 2021
SealabJaster
Jul 19, 2021
someone
Jul 19, 2021
SealabJaster
Jul 19, 2021
SealabJaster
Jul 19, 2021
someone
Jul 19, 2021
Paul Backus
Jul 19, 2021
SealabJaster
Jul 19, 2021
someone
July 19, 2021

I know the answer for this will be a resoundingly no, but, anyway ...

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

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:

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:

debugwriteln(lobjExchanges.count);

... and some way to get the identifier of what's being passed, he.

July 19, 2021

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:

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:

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

// Usage:
mixin(debugWriteln!"lobjExchanges.count");
July 19, 2021

On Monday, 19 July 2021 at 00:52:39 UTC, Paul Backus wrote:

>

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

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

// Usage:
mixin(debugWriteln!"lobjExchanges.count");

clever :)

July 19, 2021

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

July 19, 2021

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 :) !

July 19, 2021

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".

July 19, 2021

On Monday, 19 July 2021 at 03:05:24 UTC, Paul Backus wrote:

>

...

Yea, that's true.

July 19, 2021

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

July 19, 2021

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.

July 19, 2021

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

« First   ‹ Prev
1 2