Herfried K. Wagner said:
Why should the compiler raise the warning for 'ByRef' if everything seems to
work.
It's pretty clear that the compiler cannot infer the type of the parameter
in the two calls above. However, automatic type inference does not work in
all scenarios of generic parameters, not limited to cases involving optional
parameters.
You should still be able to call 'xval(Of Integer)()', 'xval(Of Form)()',
'xref(Of Integer)()', and 'xref(Of Form)()' respectively.
Ok, I agree, and I change my mind completely. Now I say VB handles the
ByRef case correctly and incorrectly diagnoses the ByVal case. Can you tell
me why I'm wrong now? (I'm not being frivolous - I'm half way there to
understanding this.)
Also, from the description of "Generic parameters used as optional parameter
types must be class constrained" comes:
"When you use a type parameter for an optional parameter, you must guarantee
that it is of a reference type to avoid the possibility of a value type with
no valid default value. This means you must constrain the type parameter
either with the Class keyword or with the name of a specific class."
Do you know of any value type where Nothing is not a valid assignment? For
example,
Dim x as SomeValueType ' if this compiles...
x = Nothing ' ...then so does this
In all cases of value types that I have looked at, x=Nothing is a valid
assignment, hence ought to be a valid default for an optional parameter.
Even an enum with no zero value still allows an assignment of Nothing.