B
Bruno Jouhier [MVP]
I hadn't considered that the code may be generated *around* the method,
but
If the code is generated "in", as if the parameter was replaced by the
expression every time it appears in the body, this is called passing "by
name", and I would be very strongly against it.
I still don't understand your issue and why a special exception would be
required. I see two cases:
"inout" parameter: the method body may or may not reassign the inout
parameter. If the method completes "normally" (without throwing the
exception), the copyout will assign the last value of the parameter. If the
parameter has not been reassigned inside the method body, nothing really
terrible happens (the unmodified value is reassigned -- a variant would be
to prevent the copy out operation in this case to avoid potential side
effects but this makes code generation a bit more complex for the method
body -- even in the second case, I don't see why an exception is needed,
this can be done with a simple flag, and even by a static analysis at
compile time in most cases).
"out" parameter: here the compiler must check that all execution paths
assign a value to the "out" parameter inside the method body (unless they
throw an exception of course), the same way it checks that all execution
paths return a value if the method has a non void return type. Here, if the
method returns "normally", it must assign a value to "out" parameter.
Bruno.
but
in, which helps mitigate much of the problem(not entirely sure why I was
thinking that way...3am responses aren't always well thought out,. The
problem still stands that you would have to throw an exception to stop that
copyout from happening. Your example itself uses an if block to determine
success, how do you escape without throwing an exception?
If the code is generated "in", as if the parameter was replaced by the
expression every time it appears in the body, this is called passing "by
name", and I would be very strongly against it.
I still don't understand your issue and why a special exception would be
required. I see two cases:
"inout" parameter: the method body may or may not reassign the inout
parameter. If the method completes "normally" (without throwing the
exception), the copyout will assign the last value of the parameter. If the
parameter has not been reassigned inside the method body, nothing really
terrible happens (the unmodified value is reassigned -- a variant would be
to prevent the copy out operation in this case to avoid potential side
effects but this makes code generation a bit more complex for the method
body -- even in the second case, I don't see why an exception is needed,
this can be done with a simple flag, and even by a static analysis at
compile time in most cases).
"out" parameter: here the compiler must check that all execution paths
assign a value to the "out" parameter inside the method body (unless they
throw an exception of course), the same way it checks that all execution
paths return a value if the method has a non void return type. Here, if the
method returns "normally", it must assign a value to "out" parameter.
Bruno.