-
Notifications
You must be signed in to change notification settings - Fork 164
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
Positional-only arguments #247
Comments
Note that the highly related PEP 3102 keyword-only arguments |
Keyword-only arguments are also supported in Bazel's implementation of Starlark and, from what I can tell, frequently used, especially in the Starlarkification efforts. Citing @laurentlb from https://bazelbuild.slack.com/archives/CDCEKVCHY/p1678902971702729?thread_ts=1678902446.157669&cid=CDCEKVCHY
I agree that the syntax isn't well known and perhaps also not intuitive. But I would consider the concept to be both well-known and intuitive. In more detail:
|
I rather like the idea of adding this feature to Starlark. We already have the concept of positional-only arguments for built-in methods defined in Bazel's Java code. And we've already run into problems where you have a **kwargs-accepting method that also takes in some fixed parameters that you don't want to name-clash with. (Ex: Imagine if we decided in the future to pass a Laurent may have a point about surprising users with confusing syntax. So we'd have to balance that risk. While I don't think this would necessarily be a difficult change to implement, it's not exactly trivial to review, given the subtleties of argument processing. And it would complicate the grammar. So this is blocked on 1) convincing evidence that it can carry its weight especially regarding not befuddling the average user, and 2) prioritization. |
I believe that https://peps.python.org/pep-0570, which describes very simple syntax for positional-only arguments, would be very useful to have in Starlark.
It does enable one to write clean and robust helper functions such as
def format_rule_call(kind, /, **attrs)
ordef replace_fields(struct, /, **replacements)
without having to worry about the edge case where a keyword argumentkind
orstruct
is passed.This can currently be worked around to some extent by giving the positional arguments an odd name (e.g.
__kind
), but that's confusing, not guaranteed not to conflict and at odds with style guides.The text was updated successfully, but these errors were encountered: