[csswg-drafts] [css-mixins-1] Named argument passing? (#11749)

tabatkins has just created a new issue for https://github.com/w3c/csswg-drafts:

== [css-mixins-1] Named argument passing? ==
Most programming languages have, in addition to passing arguments by index, a way to pass args by *name*. For example, in Python you can write `foo(x=1, y=2)` to pass values to the `x` and `y` arguments of the function, regardless of what order they appear in the function definition's arglist (or what other arguments might exist).

This is a pretty useful feature in general, and I think would be great to expose for CSS functions, especially since all of a CSS function's arguments are (implicitly) optional. It would allow, for example, patterns like this:

```css
@function --shadow-stuff(--x <length>, --y <length>, --color <color>, --shadow: var(--x) var(--y) var(--color)) {
  /* stuff involving a shadow */
}
```

That is, you could pass the shadow components individually, like `--shadow-stuff(2px, 2px, blue)`, *or* all at once as a single arg, like `--shadow-stuff(--shadow: 2px 2px blue)`. (Currently, doing the latter would require you to pass something invalid to the first args, like `--shadow-stuff(*, *, *, 2px 2px blue)`, which is silly and not very usable.)

The behavior is pretty minimal - we'd just reserve the use of a top-level colon in the arg syntax to indicate an arg name. (If you did want to write in an explicit value with a top-level colon, you could still use the `{}` wrapper, just like passing a value with a comma in it. Per the discussion earlier today in #11500, you don't have to worry about a var() accidentally containing a colon and triggering this, as the colon would be part of the "early grammar".)

Arg validity would be:
* any indexed arguments have to come before any named arguments - `--foo(--karg: 1, 2, 3)` is invalid
* if a named argument references an arg name that was *also* passed by index, it's invalid
* similarly, passing the same named argument multiple times is invalid
* passing a named argument that doesn't exist is invalid (same as passing too many indexed arguments)

In all these cases, "invalid" means the same thing as passing too many args - it causes the function to return the guaranteed-invalid value without executing anything.

Note that this implicitly exposes the arg names being used, so that's part of the function contract that authors have to be aware of. I think this is fine. In particular, Python has this as the default behavior (and the *only* behavior in Python 2) - unless you explicitly use a special syntax in your arglist (which is pretty rare in practice), all your arguments can be passed by index *or* name, using similar rules as above.

Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/11749 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Wednesday, 19 February 2025 21:23:28 UTC