Introduce Code Rewrite Rule for Optional Parameter
- Dominant language
- No language data
- Stars
- 328
- Forks
- 71
- PR merge metrics
- No merged PRs in 30d
Description
Applying the optional parameter its default value have a limiting condition in current .NET runtime: **its value should be a constant value**. So that we are unable using an expression(example like function calls) as the optional parameter its default value, because the expression produce runtime value.
This restriction makes the coding in current version of VB language inconvenient usually. For break such inconvenient restriction, we have some candidate options like:
+ [Convert the constant value to a non-primitive type object](https://github.com/dotnet/vblang/issues/19).
+ Or Just rewrite the code (in current Proposal).
The typescript language is a kind language that similar to VB language. The typescript language is also have the optional parameter for its function, example as:
```ts
module codeCompare {
export function Foo(input: CSS = defaultStyle()) : string {
// Optional parameter will be rewrite as
// if (input === void 0) { input = defaultStyle(); }
}
}
```
When we run the javascript compiler, then some interesting things happened, the illegal part of the code in javascript will be rewrite as:
```ts
function Foo(input: CSS) : string {
if (input === void 0) {
input = defaultStyle();
}
```
The VB code is also have some situation like:
```vbnet
Module CodeCompare
Public Function Foo(input As CSS = defaultStyle()) As String
End Function
End Module
```
The VB code that show above is illegal at current:
```vbnet
' Not Allowed
Public Function Foo(input As CSS = defaultStyle()) As String
```
But we usually rewrite the code as below:
```vbnet
' Allowed
Public Function Foo(input As CSS = Nothing) As String
If input Is Nothing Then
input = defaultStyle()
End If
...
```
So by borrowing this advantage idea from the typescript compiler, rewrite rule in VB code would be something like:
```
For Expression: argName As Type =
If Type Is Primitive|Enum OrElse ( = Nothing) Then
Do Nothing
Else If Is Expression Then
Rewrite As
```
If argName Is Nothing Then
argName =
End If
```
End If
```
```vbnet
' Change current compiler workflow:
Code Analysis => Code Optimization => Finally IL code
' To
Code Analysis => Code Rewrite and Optimization => Finally IL code
```
For the situation of primitive type or non-primitive type using ``Nothing`` as default value, nothing changed in the code rewrite output:
```vbnet
input As Integer = 9 ' Nothing Changed
input As Types = Types.Integer ' Types is an Enum type, Nothing Changed
input As Integer() = Nothing ' The default value is Nothing, Nothing Changed
```
But when the default value is not an constant expression:
```vbnet
' optional value is an expression
input As Integer() = {1, 2, 3}
input As Integer? = Math.Pow(3, getPower())
```
Then it could rewrite as:
> 1. For the situation of non-primitive type, if the type is a reference type:
>
> ```vbnet
> ' Sub Foo(input As Integer() = {1, 2, 3})
> ' will rewrite As
> Sub Foo(input As Integer() = Nothing)
>
> If input Is Nothing Then
> input = {1, 2, 3}
> End If
>
> ...
> ```
> 2. For the situation of primitive type or enum/structure type, it should tag a ``?`` symbol to make it as a nullable type:
>
> ```vbnet
> ' Sub Foo(input As Integer? = Math.Pow(3, getPower()))
> ' will rewrite As
> Sub Foo(input As Integer? = Nothing)
>
> If input Is Nothing Then
> input = Math.Pow(3, getPower())
> End If
>
> ...
>
> ' Syntax error
> Sub Foo(input As Integer = Math.Pow(3, getPower()))
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.