Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] optional parameter implicit type conversion #19

Open
xieguigang opened this issue Feb 21, 2017 · 2 comments
Open

[Proposal] optional parameter implicit type conversion #19

xieguigang opened this issue Feb 21, 2017 · 2 comments

Comments

@xieguigang
Copy link

2017-02-22 3
2017-02-22 2

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:

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

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:

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

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

@AdamSpeight2008
Copy link
Contributor

AdamSpeight2008 commented Feb 22, 2017

I have a couple of concerns

  • I think the metadata type wouldn't match String vs Padding
  • What if the string isn't valid? How are error in the string indicated?

Would this work alongside #29

@xieguigang
Copy link
Author

xieguigang commented Feb 24, 2017

Hi, @AdamSpeight2008

  • I not sure how the constant optional parameter default value works in the compiler, but is there any possible that if the compiler detected that user have defined an implicit CType operator for the specific type from the primitive type, then compiler will match target type with the primitive type in the meta data?
  • In my opinion, if the constant string its value is invalid (The compiler will try running the test on this CType operator using the optional parameter its default value before the compilation process, if the test fail, in other words, this operator test throws exception by using a constant value from the optional parameter), then the compiler should throw a compiler error, and stop the compilation process. Due to the reason of the optional parameter its default value is constant, so that this error is very easy to fix and detected, or user can disable it, just like the mechanism of the compiler it does on the Integer overflow checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants