DIP 1023--Resolution of Template Alias Formal Parameters in Template Functions--Community Review Round 1

jmh530 john.michael.hall at gmail.com
Tue Sep 10 14:06:10 UTC 2019


On Friday, 6 September 2019 at 12:58:33 UTC, Stefanos Baziotis 
wrote:
> Thanks Mike and jmh530.
>
> [snip]

I'm sure I could find some way to simplify this what's below, but 
I've just been thinking about Atila's concepts library and it's 
interaction with this DIP [1]. Removing the concepts-specific 
part of it, it becomes

void checkFoo(T)()
{
     T t = T.init;
     t.foo();
}

enum isFoo(T) = is(typeof(checkFoo!T));

struct Foo
{
     void foo() {}
}

I'm curious if this DIP is accepted could something like the 
following work

template Foo_able(T)
     if (isFoo!T)
{
     alias Foo_able = T;
}

void useFoo(T)(auto ref Foo_able!T x) {

}

void main() {
     Foo x;
     useFoo(x);
}

Right now, to get it to work would require writing useFoo!Foo(x) 
because the Foo is not detected to be a Foo_able!T.

If something like the above works under this DIP, while I grant I 
don't think that this resolves all of the issues that the 
concepts library tries to address, I think it would be a win for 
readability.

The big thing that the concepts library provides over what is 
above is improved error messages (as of now, it is just a 
replacement of the template constraint). It tells you why the 
argument does not fit. It seems like if the alias is transformed 
to something like below, still using the concepts library, then 
it you would still be able to get the improved error messages. 
You wouldn't need to apply the models UDA to the struct in this 
case in order to get the good error messages.

template Foo_able(T)
{
     static assert(models!(T, isFoo));
     alias Foo_able = T;
}

[1] https://github.com/atilaneves/concepts


More information about the Digitalmars-d mailing list