-
Notifications
You must be signed in to change notification settings - Fork 55
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
Consider revising function argument defaulting #27
Comments
Scala (which shares our named argument syntax) has an interesting design, which I think we can borrow from: https://docs.scala-lang.org/sips/completed/named-and-default-arguments.html |
If we apply the Scala approach in the context of Ballerina, we would end up with something like this.
|
Scala allows a named argument to occur before a positional argument in a function call, but requires that for such an argument the name and position must correspond (i.e. if argument number n has name x and occurs in a function call before a positional argument, then parameter number must also have name x). I am not sure if that is the right rule for us. |
I wonder if we need a way to write the type of a function with a defaultable argument without specifying what the default is. Not very useful because you would not be able to call the function without specifying the defaultable argument, because you would not know what to default it to. |
We should consider whether we want to allow |
For function call site, discussion suggests people prefer to have all arguments specified by position coming before all arguments specified by name. |
I think making all subsequent parameters to require a default after a parameter is given a default value will reduce the flexibility of this design. Yes, enforcing that all the positional arguments should come before named arguments is better IMO. |
@sameerajayasoma The latter rule is about the call site. The former rule is about the declaration site. I don’t see the latter as an alternative to the former. Can you give an example of when you would need flexibility prohibited by the former rule? The reason for the rule is to make it more obvious which parameter a positional argument corresponds to. Without this, people maybe unsure whether a positional argument specifies a defaultable parameter or a subsequent non-defaultable parameter. |
Kotlin’s approach seems similar https://kotlinlang.org/docs/reference/functions.html#named-arguments |
We can distinguish two different approaches, depending on whether the defaultability of a parameter is part of the shape (ie affects subtyping). The more straightforward approach is where defaultability is not part of the shape. The mapping of parameter names to positions and providing defaults for unspecified arguments is handled by the caller. The callee sees the ordered list of arguments ready to go. The typedesc conceptually provides both a parameter name-to-position map, and for each defaultable parameter a closure that computes the default value from the value of previous parameters (in a similar way to records). It has the disadvantage that this does not work:
One could have some built-in methods on the typedesc to generate wrappers automatically. With this approach subtyping is straightforward, with the rest argument being the only complication. |
Tentative conclusions after discussion between me and @sanjiva are:
|
We should consider whether and, if so, how the design of function argument defaulting should be revised so as to
The text was updated successfully, but these errors were encountered: