simple (I think) eponymous template question ... what is proper idimatic way ?

Bastiaan Veelo Bastiaan at Veelo.net
Tue Aug 17 21:39:41 UTC 2021


On Tuesday, 17 August 2021 at 20:29:51 UTC, james.p.leblanc wrote:
> So, below
> is my code:
>
> import std.stdio;
> import std.meta : AliasSeq;
>
> template isAmong(T, S...) {
>    static if (S.length == 0)
>       enum isAmong = false;
>    else
>       enum isAmong = is(T == S) || isAmong(T, S[1..$]);
> }
>
> alias MyTypes = AliasSeq!(int, float);
>
> auto myFunc(T)(T a, T b) if (isAmong!(T, MyTypes)) {
>   writeln(" in myFunc ");
>   return;
> }
>
> void main(){
>   writeln("started ...");
>    auto a = 1;
>    auto b = 2;
>    myFunc!(int)(a, b);
>    return;
> }
>
>
> And, here are the error message:
>
> (master) Notes > dmd recursive_template.d
> recursive_template.d(9): Error: circular reference to variable 
> `recursive_template.isAmong!(int, int, float).isAmong`
> recursive_template.d(17): Error: template instance 
> `recursive_template.isAmong!(int, int, float)` error 
> instantiating
> recursive_template.d(29):        while looking for match for 
> `myFunc!int`
>
> Can anyone see what is going on?
>
> Best Regards,
> James

https://run.dlang.io/is/m5svQ2

The error was in line 8.
T (Teoh) forgot to take the first of `S` in `is(T == S[0])`. The 
error message improves after adding the `!` in `isAmong!(T, 
S[1..$])`, which, surprisingly, is optional!

— Bastiaan.


More information about the Digitalmars-d-learn mailing list