Interfaces, traits, concepts, and my idea for a DIP
Atila Neves via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jul 31 07:23:26 PDT 2015
On Friday, 31 July 2015 at 11:16:48 UTC, Biotronic wrote:
> On Thursday, 30 July 2015 at 10:40:59 UTC, Atila Neves wrote:
>> There's still the problem of having two names for each
>> constraint: checkInputRange and isInputRange. But... we could
>> also establish the convention of checkXXX and use string
>> mixins to turn this:
>>
>> @satifies!(isInputRange, MyRange)
>>
>> into (which works cos I just tried it):
>>
>> template satisfies(alias Constraint, R) {
>> enum check = "check" ~ Constraint.stringof[2..$-3] ~
>> "!(R)";
>> enum assert_ = "static assert("~ check ~ ");";
>> mixin(assert_); //mixes in "static
>> assert(checkInputRange!R)"
>> }
>>
>>
>> @satisfies!(isInputRange, Zeroes)
>> struct Zeroes {
>> enum empty = false;
>> void popFront() {}
>> @property int front() { return 0; }
>> }
>>
>>
>> Now, _this_ I could go for.
>>
>> Atila
>
> Why are there two different things in the first place?
>
> @satisfies!(myConcept, T) should test the constraints and give
> a sensible error message. This you use to indicate that type T
> implements myConcept.
>
> Why can't another template use the very same concept
> information to check if a type implements the concept?
>
> e.g.:
>
> @satisfies!(myConcept, MyStruct)
> struct MyStruct { /* ... */ }
>
> void foo(T)(T t) if (check!(myConcept, T))
> { /* ... */ }
Because you want to:
1. Check your type and get sensible error messages
2. Use a constraint in function declarations
You can't have `@satisfies!(isInputRange, T)` give you an error
message if it doesn't have access to the source code of the
lambda that actually does the checking and you can't get error
messages unless you force the compilation of code you _know_
won't compile.
It's ok for template constraints to be false without causing a
compilation error; that's how we get compile-time template
function resolution. When you're using something like
`@satisfies` however, you want a compilation error and its
message. That's why you need two things.
Atila
More information about the Digitalmars-d
mailing list