-
-
Notifications
You must be signed in to change notification settings - Fork 740
Better static assertion on template contraints with 'models' #3677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
* The template contraint predicate must start with the word $(D is) | ||
* (e.g. $(D isInputRange)) and an associated template function | ||
* with the "is" replaced by "check" should exist (e.g. $(D checkInputRange)) | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the Params and Returns sections.
enum untilOpenParen = P.stringof[2 .. openParenIndex]; | ||
enum checkName = "check" ~ untilOpenParen; | ||
enum mixinStr = checkName ~ "!(T, A);"; | ||
mixin("import " ~ moduleName!(P) ~ ";"); //make it visible first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this entail massive code bloat as you're importing at least one module per check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if it was already going to fail to compile, so no.
@JackStouffer Will do |
Looks cute. Will review later ... |
It's ugly that in the UDA version you have to repeat the type's name... |
@schuetzm I agree, but there's nothing I can do about that. |
|
||
|
||
/// | ||
unittest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding in another ddoc-ed unit test in front of this with range type check examples would be helpful as that's the most common use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd have to change isInputRange
first and I wanted to make the changes orthogonal.
@JackStouffer That's not possible since |
{ | ||
bool models() | ||
{ | ||
import std.algorithm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import std.algorithm.searching : countUntil;
This sounds like something that would be good for a dub release. If it's popular and works then it can be reconsidered for Phobos. |
While I like the idea too, I am inclined to close this as there still seems to be no consensus after one year & going the DUB or DIP road make more sense than getting stalled here? @ others: What do you think? |
This is similar to the work done on this PR, which is now dependent on the current one.
The idea is to have compiler error messages when a type that was intended to satisfy a template constraint but doesn't. Currently, when a template contraint isn't satisfied by accident, it's hard to know why. This PR attempts to make that process easier, with a similar intent as C++ concepts.
Also, I wanted to add a static assert if
models
is true butcheckXXX
doesn't compile; I didn't because I'd have to move theisFoo
andcheckFoo
functions used in the accompanying unit tests to global scope.