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

Method optional arguments aren't optional with slurpy arguments #80

Closed
catb0t opened this issue Mar 3, 2019 · 2 comments
Closed

Method optional arguments aren't optional with slurpy arguments #80

catb0t opened this issue Mar 3, 2019 · 2 comments

Comments

@catb0t
Copy link
Contributor

catb0t commented Mar 3, 2019

class X {
  -> m (String x, Bool f = false, *args) {
    "x: #{x}, f: #{f}, args: #{args}"
  }
  -> u (String x, Bool f = false) {
    "x: #{x}, f: #{f}"
  }
  -> t (Bool f = false, *args) {
    "f: #{f}, args: #{args}"
  }
}
func f (n, r = 1, *y) { [n, r, y...] }

say f(:ABC, 123)
say X().u(:abc)
say X().m(:abc, f: true, :def)
say X().t(:abc)
say X().m(:abc, :def)

This feature of keyword arguments is fine on functions, I think it's meant to work on methods too. It doesn't matter whether the optional argument appears immediately before the slurpy parameter or not.

["ABC", 123]
x: abc, f: false
x: abc, f: true, args: ["def"]
[ERROR] method `t` does not match t(X, String), invoked as t(X(), "abc")

Possible candidates are: 
    X.t(self, Bool f, *args)
@trizen
Copy link
Owner

trizen commented Mar 3, 2019

This is the intended behavior. Same as in Perl 6:

sub hi ($x, $f = False, *@rest) {
    "x: $x -- f: $f -- args: [{@rest}]";
}

say hi("foo", 1234);          # x: foo -- f: 1234 -- args: []
say hi("foo", False, 1234);   # x: foo -- f: False -- args: [1234]

While with typed parameters, we have:

sub hi (Str $x, Bool $f = False, *@rest) {
    "x: $x -- f: $f -- args: [{@rest}]";
}

say hi("foo", False, 1234);  # x: foo -- f: False -- args: [1234]
say hi("foo", 1234);         # Type check failed in binding to parameter '$f'; expected Bool but got Int

@trizen trizen closed this as completed Mar 3, 2019
@catb0t
Copy link
Contributor Author

catb0t commented Mar 7, 2019

Well, that's completely bizarre, but I guess I should stop expecting the Perl language family to work like Python.

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

2 participants