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

Named parameters and ParamArray do not not play nice together #597

Open
jrmoreno1 opened this issue May 27, 2021 · 7 comments
Open

Named parameters and ParamArray do not not play nice together #597

jrmoreno1 opened this issue May 27, 2021 · 7 comments

Comments

@jrmoreno1
Copy link

jrmoreno1 commented May 27, 2021

I'm creating an extension method with 3 parameters, the last being a paramaray. When attempting to reorder the parameters using named parameters, the signature is unable to be matched.

Public Module MainModule 
    Sub Main()
      Dim b = "".Example("param1", i:=5)
    End Sub 

    <Extension>
    Public Function Example(s As String, i As Integer, ParamArray p As String()) As Boolean
        Return False
    End Function
End Module 

This gets two errors, BC30512 and BC36584.

@RevensofT
Copy link

Isn't it always has been like this ? ParamArray mostly input unknow length of argument and it must be the last set of argument.

@zspitz
Copy link

zspitz commented May 29, 2021

Confirming what @RevensofT is saying, that arguments passed into ParamArray must always be last, this also doesn't compile:

Example("abcd", i:=5)

Function Example(i As Integer, ParamArray p As String()) As Boolean
    Return False
End Function

@jrmoreno1
Copy link
Author

@zspitz : yes, they have to be last, that is what I meant by not playing nicely together. The reason why ParmaArrays have to be last is that there is no other way of deciding when the parameters end UNLESS named arguments are used, in which case, all named arguments could be removed from consideration.

Before named parameters, given a call to fn(integer, paramarray integer, integer, integer) it would have been impossible to determine whether fn(1,2,3) was supposed to represent fn(1, {}, 2,3) with an empty parameter array, or whether it was supposed to represent fn(1, { 2}, 3) where the last parameter was left off by mistake, or even fn(1, { 2,3}) and the last 2 were left off by mistake , with named parameters, the paramarray still needs to be declared last, eg sub fn(first as integer, second as integer, third as integer, paramarray i() as integer), but calling it as fn(4,5,6,third:=3, second:=2, first:=1) is no longer ambiguous.

With named parameters, paramarray parameters should be more flexible.

@gilfusion
Copy link

Correct me if I'm wrong (only verified with a quick LINQPad check), but I think that the rule is that any unnamed parameters still need to be in order. For example, if the first parameter in the call is unnamed, it needs to be the actual first parameter. If you use a named version of the first parameter later, it's marked as an error because you're assigning the same parameter twice, and this behavior is not exclusive to methods with ParamArray.

@jrmoreno1
Copy link
Author

@gilfusion : you can’t use a named parameter with param array.
vb.net Dim b = Example("", i:=5, p:= {"param1"})

which has all of elements in the correct order with the correct types, will fail, because you can’t use a named parameter with paramarray.

@Halofreak1990
Copy link

@jrmoreno1 interestingly, Visual Studio does not warn you of this construct, leaving it to the VB compiler to complain you're doing it wrong

@jrmoreno1
Copy link
Author

@Halofreak1990 : c# allows you to use named parameters and even reorder them, but isn’t smart enough to figure out that it can determine that the remaining items must be part of the params , by eliminating the other parameters.

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

No branches or pull requests

5 participants