Could Guard methods return a value? #259
Replies: 4 comments 2 replies
-
Ah, I just saw #187 that pretty much asked exactly the same question. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
FYI @Sergio0694 |
Beta Was this translation helpful? Give feedback.
-
As others have mentioned, they could return a value, but:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Per the docs,
Guard
methods 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
Guard
methods 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.
Beta Was this translation helpful? Give feedback.
All reactions