concepts v0.0.6: use a run-time interface to specify a compile-time one

Atila Neves via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Wed May 10 03:53:59 PDT 2017


http://code.dlang.org/packages/concepts

concepts is a dub package and library that allows one to declare 
that a struct conforms to a "compile-time interface" such as 
`isInputRange`. The difference between this and a simple `static 
assert(isInputRange!MyType)` is that when the static assert fails 
you have no idea why not and half of the time it's because of a 
typo (e.g. `popFrnt`). With concepts you get an error message 
from the compiler, and you can still use your concepts in 
template constraints.

It also has its own version of `isInputRange` et al that are 
drop-in replacements for the ones in Phobos, but you get compiler 
error messages instead of nothing.

The new addition is `@implements`:

interface IFoo {
     int foo(int i, string s) @safe;
     double lefoo(string s) @safe;
}

@implements!(Foo, IFoo)
struct Foo {
     int foo(int i, string s) @safe { return 0; }
     double lefoo(string s) @safe { return 0; }
}

// doesn't compile: Foo doesn't "implement" IFoo
/*
@implements!(Oops, IFoo)
struct Oops {}
*/

// also doesn't compile because foo is @system:
/*
@implements!(Nearly, IFoo)
struct Nearly {
     int foo(int i, string s) @system { return 0; }
     double lefoo(string s) @safe { return 0; }
}
*/


More information about the Digitalmars-d-announce mailing list