Open AdamSpeight2008 opened 7 years ago
This may only matter in relation to documentation, but in your examples you include the statements:
Foo( Optional arg4 As Integer ) ' --> Foo( Optional arg4 As Integer = Nothing )
Foo( Optional arg5 As Integer = 0 ) ' --> Foo( Optional arg5 As Integer = 0 )
If I understand correctly, those statements are identical, since Dim i as Integer = Nothing
is functionally equivalent to C#'s default
keyword, not its null
keyword (appreciating that this isn't a C# discussion).
As such, technically, the example might be:
Foo( Optional arg4 As Integer? ) ' --> Foo( Optional arg4 As Integer? = Nothing )
Foo( Optional arg5 As Integer ) ' --> Foo( Optional arg5 As Integer = 0 )
For the avoidance of doubt, I'm not suggesting that this adversely affects the proposal, only its documentation.
As someone who occasionally switches between teams that work in C# and VB, I know that the expectation of Nothing
being functionaly equivalent to null
catches people out more than you might hope.
@jimbobmcgee
In vb I think of Nothing
as equivalent to Default(Of T)
.
Class Type => Nothing (aka null reference)
Structure Type => Nothing ( bit pattern of structure is all zeros)
Nullable Type => .HasValue := False, .Value:= 0 ( InvalidOperation_NoValue exception if called)
The documentation is correct in terms of those examples. They are demonstrating what the feature actually get lowered to. I didn't put in an example with a nullable type, as can derived from the structure type example.
@AdamSpeight2008
I agree that Nothing
is equivalent to default(T)
, but I've seen many people forget it in the wild. To that end (and only that end), I was pointing out where the examples you had given might confuse the issue.
In terms of what Roslyn/the compiler does -- lowering, etc. -- I am not qualified to comment. If your documentation is intended only to cover that, e.g. for internal use, you'll get no argument from me!
But for any public documentation of the feature, I would not imply a difference between Nothing
and 0
as in your arg4
/arg5
examples above; and I would introduce an example that covers a Nullable type.
@jimbobmcgee I agree that public documentation should be more complete. My proposals are more on technical side, and assume a little knowledge of how it may be implemented in the compiler.
I would like to have the keyword "optional" itself optional: optional i as integer = 0 -> i as integer = 0 the "= 0" indicates that it must be optional
@MarcusMM2 Could you make that a separate proposal as the referred to proposal has already be approved by VB / Roslyn Team.
(Ported from Roslyn Repo)
The grammar definition for an
Optional Parameter
is something similar to this.Examples
It will simplify the common cases of optional parameter.
Link to Proposal #13