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

Unable to append variable-length parameters after optional parameter #980

Closed
davidkpiano opened this issue Mar 23, 2015 · 3 comments · Fixed by #981
Closed

Unable to append variable-length parameters after optional parameter #980

davidkpiano opened this issue Mar 23, 2015 · 3 comments · Fixed by #981

Comments

@davidkpiano
Copy link

A function that is called with an optional parameter followed by a variable-length parameter throws the error "Optional parameters may not be combined with variable-length parameters" despite being valid in Ruby Sass.

@function foo($value, $default: 13, $args...) {
  $res: $value + $default;
  @if length($args) != 0 {
    $res: $res + nth($args, 1);
  }
  @return $res;
}

.test {
  value: foo(3); // expected: 16
  value: foo(3, 4); // expected: 7
  value: foo(3, 4, 5, 6); // expected: 12
}

// RUBY SASS
.test {
  value: 16;
  value: 7;
  value: 12;
}

// LIBSASS
// Throws "optional parameters may not be combined with variable-length parameters"
@mgreter
Copy link
Contributor

mgreter commented Mar 23, 2015

I guess this has changed in ruby sass without us noticing. I just had to remove the existing check and everything seems to work correctly (#981). I took the liberty to adjust the test case from @davidkpiano above and I hope someone would volunteer to create a PR on sass-specs for this, thanks!

@xzyfer
Copy link
Contributor

xzyfer commented Mar 28, 2015

We need to be careful here. Optional and variable-length parameters can be mixed, but only in specific orders i.e. variable-parameters can follow optional parameters but not the other way around.

@ncoden
Copy link

ncoden commented Jun 3, 2018

variable-parameters can follow optional parameters but not the other way around.

@xzyfer Why not ? I understand that when passed inline we cannot split optional parameters from variable-parameters, but it would work well with named parameters, and would be really useful to provide additional "second-hand" options to a function.

For example:

@function please-call-this-for-me($func, $args..., $warn: true) {
    @if type-of($func) == 'function' and function-exists($func) {
        @return call($func, $args...);
    }
    @else if $warn == true {
        @warn 'Function #{$func} does not exists';
    }
}

// Call "foo" with (bar, baz, warn)
$_: please-call-this-for-me(foo, bar, baz, warn);

// Call "foo" with (bar, baz)
$_: please-call-this-for-me(foo, bar, baz, $warn: false);

That's a stupid example I know, but we can see that the "main job" of please-call-this-for-me is to call a function with arguments (this is why using variable-parameters seems legit to me, instead of a list) and that we need beside this "main job" to customize it with optional arguments that will not be used often but that we still need.

What do you think ?

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

Successfully merging a pull request may close this issue.

4 participants