Skip to content

Added Flag template #94

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

Closed
wants to merge 5 commits into from
Closed

Added Flag template #94

wants to merge 5 commits into from

Conversation

andralex
Copy link
Member

In order to make Phobos calls self-explanatory, we have a lot of small enums defining "no" and "yes". This request systematizes that approach by making it easy to use such flags without needing to define them. Furthermore, code outside Phobos can define and use flags of their own.

@andralex
Copy link
Member Author

Acted on suggestions.

@andralex
Copy link
Member Author

I must add this makes me particularly proud of eliminating parens from one-argument templates :o).

@dnadlinger
Copy link
Member

I'm not really sure what to think about this – not that the implementation wouldn't be nice, but in my opinion, introducing named parameters would be the better solution to this problem. I vaguely recall a discussion on the topic, was there any reason for not adopting them? Even C# has them now, and in my eyes they would make a hell lot of sense for D as well…

@jmdavis
Copy link
Member

jmdavis commented Jun 10, 2011

It's a bit more verbose than the current solution (e.g. Flag!"KeepTerminator".yes rather than KeepTerminator.yes), which I don't particularly like, but it does eliminate a fair bit of small bits of boilerplate code. It also standardizes this particular design pattern a bit, which is nice. Overall, I'm in favor.

As for named arguments, this can work with them just fine, though if we get named arguments at a later date and decide that we want to switch everything to bool, then we can just switch everything over then. Doing this now will reduce the boilerplate code in Phobos and encourage the use of this particular design pattern.

@michelf
Copy link

michelf commented Jun 10, 2011

I find it a little surprising to see something named "Flag" be simply a boolean in disguise. I'd have expected it to be more generic, something letting you to put multiple flags in a single value for instance, as you can find in many APIs.

Also, I agree with klickverbot: named parameters would be much better for this. The goal seems to be to compensate for a lack of expressiveness in the language, but this solution being limited to booleans and having a redundancy at the definition site (Flag!"Name" name) makes it look as if something is missing. And there's also the issue that everywhere you use it you need to name the parameter type Flag!"Name", which is just ugly. It's a clever hack for sure, but not something I'd tell people to use in regular circumstances.

@andralex
Copy link
Member Author

Michel: it's "flag", not "flags". I'll answer the other point in the newsgroup.

@dnadlinger
Copy link
Member

If this goes in, we really need to do something about the quality of error messages:

void foo(Flag!"bar" bar, Flag!"baz" baz) {}

void main() {
  foo(Yes.bar, No.bar);
}
Error: function foo (Flag bar, Flag baz) is not callable using argument types (Flag,Flag)
Error: cannot implicitly convert expression (opDispatch()) of type Flag to Flag

@andralex
Copy link
Member Author

Good point. Once the compiler improves these messages, other cases will be improved too.

@andralex
Copy link
Member Author

Too controversial.

@andralex andralex closed this Jun 12, 2011
kuettler pushed a commit to kuettler/phobos that referenced this pull request Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants