Proposal: Null propagating short-circuiting arguments #444
Unanswered
AlexanderMorou
asked this question in
General
Replies: 2 comments
-
See #433 |
Beta Was this translation helpful? Give feedback.
0 replies
-
I'm more partial to the double question mark ( Perhaps naming it Null Short Circuiting Arguments is more appropriate. |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
Null Propagating Short Circuiting Arguments
Summary
The ability to short-circuit a method-invocation in the event a specific argument is null.
Proposal
Take the following code which checks the arguments into a method prior to executing a call:
Using short-circuiting arguments the code could be simplified to the following:
Considerations
If the short-circuited argument is non-nullable, it would be incorrect to perform the operation against that parameter.
Evaluation with respect to Coercion
If there is an implicit operator which converts a type
A
, to a typeB
, it would need applied before the short-circuit operation. As a result it may impact the above consideration if the typeA
was astruct
, and typeB
is aclass
, in the event that the implicit conversion yielded a null value.Syntax
The syntax change would be fairly simple:
The
'??'? '<|'
is derived from the #96 : Proposal Pipe-Forward operatorIt wouldn't be allowed on the out parameter because it's implicitly unassigned within the scope of the called method, so it wouldn't really make sense.
Scope
The scope of the short-circuit would be to the immediate consumer of the result expression. For example:
Console.WriteLine
would be invoked each time.string.Join
would be skipped and adefault(string)
would be placed on the stack if eitherseparator
orvalues
arenull
.This would also likely require a variable for each parameter passed into the target of the method invocation, even for those which are not short-circuited. Rationale: maintaining order in which the operations are performed is paramount to avoiding altering the resulted code's behavior.
The caller of the method would need to understand the implications of using the short circuiting.
For instance:
Due to the notion of it being a short-circuiting behavior, if
B
wasnull
, expressionsA
andB
would be the only two expressions evaluated; however ifB
is non-null
andD
isnull
, expressionsA
,B
,C
, andD
would be evaluated. IfB
andD
are non-null
andG
isnull
, then all expressions would be evaluated, but Method would not be invoked.As such, the above method call would be equivalent to:
If the result of the method is not nullable, it would need to be translated into a nullable type, and the result would then be the
default(T?)
in that scenario. The rationale here is there would be no way do 'know' if the short circuiting occurred with a value type otherwise.Beta Was this translation helpful? Give feedback.
All reactions