-
Notifications
You must be signed in to change notification settings - Fork 66
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
Introduce Code Rewrite Rule for Optional Parameter #376
Comments
Given the way parameters are passed, and default parameters differ between javascript and vb.net, that can’t work, at least not as described. What could work, would be to have static method with a munged name and some attribute, which could be called to produce the optional value. Not really sure what the benefit would be.. |
@jrmoreno1 I think you're not getting the concept @xieguigang is proposing. Currently, the default value of an optional method parameter is limited to constant values only. So you cannot write code like this:
This is flagged because
The proposal is to have the IDE not flag I cannot speak for every developer out there but if implemented, this would certainly be a useful feature to me as I often encounter this issue in both VB.NET and C#. |
@franzalex I think what @jrmoreno1 is referring to is that in .NET, the value for optional parameters are inserted at compile time, whereas in TypeScript/JavaScript they are resolved at runtime. So in JavaScript, you can tell whether a value was actually passed for the optional parameter, whereas in .NET, you're always given a value for that parameter. There's two issues I can see with this. First, using your example function, if I call it like this: DoSomething() Then it would be expected that But, if I actually specify DoSomething(Nothing) Then it could be thought that So there could be some confusion because Now here's the bigger issue. It doesn't work for structures. Consider this: Public Sub DoSomehing(Optional value As Date = #2019-01-21#)
' method code here
End Sub
' Converted to:
Public Sub DoSomething(Optional value As Date = Nothing)
If value = Nothing Then value = #2019-01-21#
' method code here
End Sub If I don't pass a value for the parameter then I get DoSomething(Date.MinValue) Then I do like this idea, but the fact that it won't work with structures is a problem. |
@reduckted: exactly, although it’s actually the same issue for both classes and structures. The problem of initialization could be solved with a new keyword which instructed the compiler to create a new value using the default (parameterless) constructor at the call site. That wouldn’t work for interfaces or classes without a public parameterless ctor. If you just want to have the default value replaced with a new value, that’s just a tiny bit of boilerplate that isn’t really compelling. I find that most of the time when I am adding an optional parameter it is because I am refactoring, and adding that bit of code is effectively the same thing — I don’t have any places where it is being called with nothing, so I just add the creation to the method myself. And most of the time it is more complex than a simple new T(). |
This is a discussion about optional parameters, which presumably means sometimes calling a method without specifying a value, and sometimes with, but resolvable at compile time. An explicit value should never be treated the same as a missing value. Ahhh, for an IsMissing(parameterName) function.... Is there some way that Missing.Value could be harnessed to assist with this? (I don't know how it gets used at the moment, except from the reflection example in the online help). For trivial cases, you could decompose methods into overloads, but not so much for methods with many optional parameters. |
@pricerc: resolved at compile means that the missing parameter is added by the compiler, after that since there isn't a IsMissing function, from the point of view of the method with the missing parameter, missing and provided parameters are indistinguishable. Now that you've pointed it out, I wonder if the Missing.Value isn't what keeps classes from working with anything except a null value. |
Possible use for #282 |
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:
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:
When we run the javascript compiler, then some interesting things happened, the illegal part of the code in javascript will be rewrite as:
The VB code is also have some situation like:
The VB code that show above is illegal at current:
But we usually rewrite the code as below:
So by borrowing this advantage idea from the typescript compiler, rewrite rule in VB code would be something like:
For the situation of primitive type or non-primitive type using
Nothing
as default value, nothing changed in the code rewrite output:But when the default value is not an constant expression:
Then it could rewrite as:
The text was updated successfully, but these errors were encountered: