-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Bool arguments as switches (similar to PowerShell's switch parameters) #1462
Comments
Your example is already valid C#: void Foo(int a = 0, int b = 0, bool bar = false, bool baz = false) {}
...
int bar = 1, baz = 2;
Foo(+bar, -baz); |
@bondsbw Sorry about that, I forgot to add that I picked |
I like this concept if we can come up with good, unambiguous syntax. |
C# has its own syntax. What benefits would this addition provide? |
@MkazemAkhgary it will be just a syntactic sugar to make the method call take less space and read better. As @bondsbw said above, it can also encourage self documenting code. People who gave a thumbs-down, can you explain why? Do you think it's unnecessary, or…? If it's something specific, can it be improved? A constructive comment is better than a bad reaction. |
It's less necessary but breaks existing codes. |
@ufcpp How does it break existing codes exactly? It will still compile to what we write today. It's just syntactic sugar. If you mean the |
APIs evolve over time, and every long lived codebase I've worked on has found that methods with bool parameters (and return types) are a problem to evolve. Generally speaking, when reviewing code, if I see you have a method taking a single bool, I'll suggest you split it into two methods. For example, I'd suggest The TPL method FWIW, I've found method signatures with bool to be such an antipattern that I've blogged about it multiple times:
(Providing the links seems more efficient than rehasing the same old arguments anew; if this is unwelcome in this repo, I'll happily remove them from this comment.) |
👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍 |
@ufcpp I already replied to that. The prefix isn't @theunrepentantgeek Well, the method can take as many About your blog posts: 2 of them are for return values so I won't address them because they don't have anything against the proposal. And the first one is basically the same as what you said about splitting single-bool methods. So, the last one: You're right, that many bool parameters are absurd and using an enum is the correct way to go. But they don't have to be that many parameters. Or even if there were, calling the method with switches would be shorter than using enums, and more readable than I don't know it seems like this way could be a middle ground. It's not that much work, so I thought I'd take a shot. I'm just trying to defend my idea, I'm not sure I make sense 😄 |
If nothing else, create a |
That's what I might do today. But we are building a type for the exclusive purpose of being a set of parameters to one method - seems like an anti-pattern in itself. I don't advocate that in other situations, because method parameters are a good feature of the language. The reason we advocate it with |
Backtick is a possible unary operator for this proposal: Type.GetType(typeName, `throwOnError, `ignoreCase); |
I suggest limiting switch parameters to No need for the |
@bondsbw You're right about the |
Good on you for taking a shot - the whole idea of this repo is to discuss potential language enhancements. Inevitably, most of them will go nowhere, but all of them will inform the folks in charge of the language about what their users think is important. |
If only that were true... :( |
@MgSam I'm convinced it is true. #ymmv |
Description
When calling a method that takes a
bool
parameter, if we want to pass intrue
, instead of using a named argument likeFoo(bar: true)
, we'll be able to use the parameter's name by itself with a prefix (TBD) likeFoo(#bar);
. (multiple parameters will benefit more)The decision of the prefix is a part of the discussion.
Optional bonus syntax
When defining a method, instead of using an optional parameter like
void Foo(bool bar = false)
, we'll be able to use theswitch
keyword likevoid Foo(switch bar)
.Code
The prefix: TBD (currently using
#
)The text was updated successfully, but these errors were encountered: