Ambiguity issue with expanding and evaluating single template type parameter enums

data pulverizer data.pulverizer at gmail.com
Tue Dec 28 00:13:13 UTC 2021


On Monday, 27 December 2021 at 23:04:40 UTC, Adam Ruppe wrote:
> On Monday, 27 December 2021 at 21:21:30 UTC, data pulverizer 
> wrote:
>>   alias T = MyType!(INTEGER);
>
> What is MyType?
>
>>   enum code = "writeln(\"instance: \", adder(" ~
>>               T.stringof ~ "(), " ~ U.stringof ~ "()" ~ "));";
>
> And why is this a string mixin instead of a plain simple 
> function?
>
> prolly need more context....

Sorry the example is a bit contrived but basically I'm generating 
a whole bunch of code using string mixins.

The types I'm generating are a template type I've constructed for 
R's SEXP, so that my wrapped numeric vector (struct) type is 
denoted `RVector!(REALSXP)`. But `alias REALSXP = 
SEXPTYPE.REALSXP` where `SEXPTYPE` is an `enum`.

So if I start using `T.stringof` where `T = 
RVector!(SEXPTYPE.REALSXP)` to generate code it starts to create 
chaos because `T.stringof = "RVector!SEXPTYPE.REALSXP"`, so if 
I'm trying to convert or instantiate a type using `T.stringof ~ 
"(x)"`, I'll get `RVector!SEXPTYPE.REALSXP(x)` which gives an 
error, and various types like this can occur many times in a 
script. The new template allows me to safely paste the type and 
get what I want `RVector!(SEXPTYPE.REALSXP)(x)`.

There are various requirements, sometimes I have to cast or type 
convert, so I **need** the type to paste correctly and 
explicitly. Which is what the `safe_stringof` template does for 
my baby example - the same methodology will work just as well for 
my `RVector` code.





More information about the Digitalmars-d-learn mailing list