Generating unique identifiers at compile time

2022-06-09 Thread JG via Digitalmars-d-learn

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?







Re: Generating unique identifiers at compile time

2022-06-09 Thread user1234 via Digitalmars-d-learn

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


Re: Generating unique identifiers at compile time

2022-06-09 Thread bauss via Digitalmars-d-learn

On Thursday, 9 June 2022 at 23:50:10 UTC, user1234 wrote:


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


You can generate unique names actually by using CTFE RNG, so it's 
not really necessary to have it exposed to achieve that.


See my hacky example of hidden class members here:

https://forum.dlang.org/thread/siczwzlbpikwlevvi...@forum.dlang.org