Generating unique identifiers at compile time
user1234
user1234 at 12.de
Thu Jun 9 23:50:10 UTC 2022
On Thursday, 9 June 2022 at 21:20:27 UTC, JG wrote:
> Hi,
>
> As an experiment I have implemented the following kind of
> pattern matching
> (by parsing the part of the string before '=').
>
> ```d
> struct S {
> int x;
> int y;
> }
>
> struct T {
> int w;
> S s;
> }
> void main()
> {
> mixin(matchAssign(q{auto T(first,S(second,third)) =
> T(1,S(2,3));}));
> first.writeln; // 1
> second.writeln; // 2
> third.writeln; // 3
> }
> ```
> In doing so I wanted to produce unique identifiers (something
> like gensym in racket.) I did this in a very hacky way:
>
> ```d
> auto hopefullyUniqueName(size_t n, size_t line=__LINE__) {
> return
> format!"____________________________var___________________________%s%s"(n,line);
> }
> ```
>
> where I pass in the hash of the right hand side in the
> assignment in place of n.
>
> Is there some way to ask the compiler for a unique name or a
> better way of achieving this?
No, for now there if there are other ways they are as hacky as
yours.
The compiler usually uses a global counter to generate
temporaries.
There's [been attempts] to expose it, exactly so that users can
generate unique names, but that did not found its path in the
compiler.
[been attempts]: https://github.com/dlang/dmd/pull/10131
More information about the Digitalmars-d-learn
mailing list