What is the shortest way to define an immutable token of unique type?

Adam D. Ruppe destructionator at gmail.com
Sun Mar 14 19:23:33 UTC 2021


On Sunday, 14 March 2021 at 19:10:19 UTC, Petar Kirov 
[ZombineDev] wrote:
> Unfortunately, unlike struct values, classes objects can't pass 
> through from CTFE to RT, which makes them limited for certain 
> use cases (e.g. you can't put the statement above at the module 
> scope).

Yes they can. The limitation is class objects cannot be enums and 
passing as template arguments is not allowed (I believe the error 
is caught at codegen though so avoid codegen, avoid error, but 
still).

But you can assign them to static values post ctfe.


I think you could make a little function to convert a class to a 
struct too

	immutable bar = new class { int a; }.toStruct;

That is totally possible, but the implementation of toStruct is a 
slight pain, keeping the identifiers and such. I don't think it 
is worth the hassle.


More information about the Digitalmars-d mailing list