Yes, constraints can have helpful error messages!
Marvin Hannott
i28muzw0n at relay.firefox.com
Fri Apr 15 16:39:48 UTC 2022
On Friday, 15 April 2022 at 10:07:10 UTC, Atila Neves wrote:
> Have you looked at https://github.com/atilaneves/concepts ?
I have, but, ehm, to be honest, I don't quite understand how this
is different from just not using constraints at all. Referring to
your example:
```D
import concepts.models: models;
void checkFoo(T)()
{
T t = T.init;
t.foo();
}
enum isFoo(T) = is(typeof(checkFoo!T));
@models!(Foo, isFoo) //as a UDA
struct Foo
{
void foo() {}
static assert(models!(Foo, isFoo)); //as a static assert
}
// can still be used as a template constraint:
void useFoo(T)(auto ref T foo) if(isFoo!T) {
}
```
I mean, where's the difference between `checkFoo(T)` failing or
`useFoo(T)`? And to check that a type satisfies a constraint with
`static assert` can be done with only `checkFoo(T)`, letting it
return `true`.
Being able to implement compile-time interfaces is really cool,
though.
And I apologize if I am getting this completely wrong. Sometimes
it can be difficult to immediately see why something is useful.
More information about the Digitalmars-d
mailing list