dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
289 stars 66 forks source link

[Proposal] Implicit Default Optional Parameter #29

Open AdamSpeight2008 opened 7 years ago

AdamSpeight2008 commented 7 years ago

(Ported from Roslyn Repo)

The grammar definition for an Optional Parameter is something similar to this.

OptionalParameter ::= "Optional" ParameterName Typing? DefaultToValue?
           Typing ::= "As" TypeIdentifier
   DefaultToValue ::= "=" ( "Nothing" | ConstantValue )

Examples

Foo( Optional arg0 ) ' --> Foo( Optional arg0 As Object = Nothing )
Foo( Optional arg1 As String ) ' --> Foo( Optional arg1 As String = Nothing )
Foo( Optional arg2 As String = Nothing ) ' --> Foo( Optional arg2 As String = Nothing )
Foo( Optional arg3 = "" ) ' --> Foo( Optional arg3 As String = "" )
Foo( Optional arg4 As Integer ) ' --> Foo( Optional arg4 As Integer = Nothing )
Foo( Optional arg5 As Integer = 0 ) ' --> Foo( Optional arg5 As Integer = 0 )
Foo( Optional arg6 As Integer = 1 ) ' --> Foo( Optional arg6 As Integer = 1 )
Foo( Optional arg7 = 7 ) ' --> Foo( Optional arg7 As Integer = 7 )

It will simplify the common cases of optional parameter.

Link to Proposal #13

jimbobmcgee commented 5 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.

AdamSpeight2008 commented 5 years ago

@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.

jimbobmcgee commented 4 years ago

@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.

AdamSpeight2008 commented 4 years ago

@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.

MarcusMM2 commented 4 years ago

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

AdamSpeight2008 commented 4 years ago

@MarcusMM2 Could you make that a separate proposal as the referred to proposal has already be approved by VB / Roslyn Team.