Could Guard methods return a value? #259
prlcutting
started this conversation in
Ideas
Replies: 4 comments 2 replies
|
Ah, I just saw #187 that pretty much asked exactly the same question. |
0 replies
|
It would be nice but it would be a very significant breaking change to method signatures. It also defeats one of the main use purposes of There is an open proposal for a partner API called |
1 reply
|
As others have mentioned, they could return a value, but:
|
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Per the docs,
Guardmethods all returnvoid. For the typical use case of evaluating a parameter and then assigning it to a backing field or property when using constructor injection, the pattern looks like this:This code could be a little terser if the various
Guardmethods returned a value, such that the parameter could be validated and an exception thrown if the validation fails, otherwise assigned to the member/variable all in one step, e.g.:Of course this would not preclude the first pattern as the return value could just be ignored if not needed/desired.
Was this use case considered? I'm sure it was, and there was a reason for not doing this - perhaps performance related? If there is merit to this idea, could this be implemented in a non-breaking manner?
Note: the inspiration for this API idea was drawn from Steve Smith's excellent Ardalis.GuardClauses library.
All reactions