Named parameters

Currently when you call a method in Xojo the parameters are matched up starting from the first to the last from left to right. They are matched up based on their position in the argument list.

One thing some other languages support is “named parameters”.

What this allows is a certain degree of flexibility that doesn’t exist with positional parameters like Xojo uses.

For instance in Xojo if you have a method, Foo, with parameters as follows

Sub Foo(a as integer, b as string, c as Date)

you can call it only using the correct types of parameters in the correct order like

Foo(123, "345", new Date)

There are ways, with optional parameters, to modify this normal operations. If that method was defined as

Sub Foo(a as integer = -1, b as string = "", c as Date = nil)

You could call this like

Foo()
Foo(123)
Foo(123, "345")
Foo(123, "345", new Date)

All the parameters are optional and any that are not supplied a default value as shown in the declaration. But everything is still positional. And there are things you cannot do like skip one parameter. None of these will compile.


Foo(, ,New Date)
Foo( , "345" ,New Date)
Foo(123, ,New Date)

So how would named parameters help ? Named parameters can let you say, explicitly, “this value is for this parameter” rather than just matching up by position. For instance out call to Foo might look like

Foo()
Foo( a:123)
Foo( a:123, b:"345")
Foo( a:123, b:"345", c:new Date)

And you might also be able to do something like

Foo( b:"345", a:123 )
Foo( a:123, c:new Date, b:"345")

And not have to worry about the order. And this might let you do something like

Foo(c:New Date)
Foo( b:"345" , c:New Date)
Foo(a:123, c:New Date)

which skips certain parameters in a manner that you cannot do today.

Certainly this would not be a trivial change to Xojo to support positional and/or named parameters. But there are languages that do this.

In the mean time its possible to manually create your own support for “named parameters”.

If we create Foo with two signatures like

Sub Foo(a as integer = -1, b as string = "", c as Date = nil)
End Sub
Sub Foo(paramarray values as Pair)
End Sub

Then Foo could be called as

Foo(123, "345")
Foo(123, "345", New Date)
Foo( "c":New Date)
Foo( "b":"345" , "c":New Date)
Foo( "a":123, "c":New Date)

NOTE that the “names” of the parameters have to be a literal type – in this case a string.

In this set up though we cant use no parameters. So let’s alter the set up to have at least one parameter otherwise the compiler will have an ambiguous overload in the case there are no parameters. It couldn’t distinguish between the call to Foo where it should use all default parameters and an empty paramarray. So now our declarations look like

Sub Foo(a as integer = -1, b as string = "", c as Date = nil)
End Sub
Sub Foo(value as pair, paramarray values as Pair)
End Sub

And we’ll be able to call this like

// these are positional
Foo
Foo(123)
Foo(123, "345")
Foo(123, "345", New Date)

// the use "named" parameters
Foo( "c":New Date)
Foo( "b":"345" , "c":New Date)
Foo( "a":123, "c":New Date)

So we’re on our way to being able to use either positional parameters OR named parameters. Now how do we make use of the overload that supports named parameters to call the one that supports positional parameters ?

In this method we need to

  • make sure we declare one local variable of the same type for each positional parameter
  • make sure we set these local variables default value to the same default values as the version that uses positional parameters
  • process the FIRST pair and all subsequent pairs in the same fashion and assign values to the local variables

Following all those admonitions the code in the version of foo using named parameters looks like

Dim a As Integer = -1
Dim b As String 
Dim c As Date

Select Case value.Left
Case "a"
  a = value.Right.IntegerValue
Case "b"
  b = value.Right.StringValue
Case "c"
  c = value.Right
End Select

For i As Integer = 0 To values.ubound
  
  Select Case values(i).Left
  Case "a"
    a =  values(i).Right.IntegerValue
  Case "b"
    b =  values(i).Right.StringValue
  Case "c"
    c =  values(i).Right
  End Select
  
Next

foo(a, b, c)

There are caveats to this technique though. Positional parameters will be checked by the compiler for type, the “named parameter” version will only be checked that the passed items ARE pairs. The names and values contained by those pairs are variants and so can be anything. And you can pass the same named parameter multiple times since the compiler is unaware of named parameters. And you can’t use “byref” parameters with the named parameter version – you COULD use reference types though.

I’d love to see Xojo support named and positional parameters using a syntax that looks like “pairs” as first presented.

In the mean time this is what we have.