Thread overview | |||||
---|---|---|---|---|---|
|
May 10, 2017 concepts v0.0.6: use a run-time interface to specify a compile-time one | ||||
---|---|---|---|---|
| ||||
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; } } */ |
May 10, 2017 Re: concepts v0.0.6: use a run-time interface to specify a compile-time one | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Wednesday, 10 May 2017 at 10:53:59 UTC, Atila Neves wrote:
> concepts is a dub package and library that allows one to declare that a struct conforms to a "compile-time interface" such as `isInputRange`.
Awesome!
|
May 11, 2017 Re: concepts v0.0.6: use a run-time interface to specify a compile-time one | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Wednesday, 10 May 2017 at 10:53:59 UTC, Atila Neves wrote:
> 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.
Niiiiice, I love constraints but there error messages aren't the best.
|
Copyright © 1999-2021 by the D Language Foundation