dotnet / dotnet/vblang

[Proposal] optional parameter implicit type conversion

Open
#19 2 comments 1 reaction 0 assignees View on GitHub
Discussion
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

![2017-02-22 3](https://cloud.githubusercontent.com/assets/9410171/23188581/3169f8e8-f8ca-11e6-8415-bac68d0d8ebd.png)
![2017-02-22 2](https://cloud.githubusercontent.com/assets/9410171/23188594/3a477116-f8ca-11e6-9ccf-dcc17b821f22.png)

Due to the reason of the optional parameter its value required of the primitive type constant value, so that there is no way for using non-primitive value as the optional parameter its default value. So that this example code will throw the compiler error:

```vbnet
Sub Plot(Optional padding As Padding = New Padding(100, 100, 100, 100))
...
End Sub
```

But if assuming that we have define the ``CType`` implicit conversion for our non-primitive type creates from a primitive type value, then is there any possible to apply this implicit conversion on the optional parameter?

![2017-02-22 5](https://cloud.githubusercontent.com/assets/9410171/23189525/2bc45ac4-f8ce-11e6-8cc2-894502281477.png)

For example, we have creates a ``CType`` implicit conversion for type ``Padding`` convert from ``String`` type, so that we can using a constant string value ``padding: 100px 100px 100px 100px;`` as its optional parameter default value:

```vbnet
Sub Plot(Optional padding As Padding = "padding: 100px 100px 100px 100px;")
...
End Sub
```

Due to the reason of the optional parameter ``padding`` its default value is a string and this string value is also constant, so that this will not violate the rule of the optional parameter should be constant. and this default value can be stored in the compiled assembly as meta data as other optional parameter does.

![2017-02-22](https://cloud.githubusercontent.com/assets/9410171/23188609/4febc51c-f8ca-11e6-9a2d-273cb6698d0c.png)

This new feature will makes the VisualBasic coding more convenient when the function have a lot of the optional parameter.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.