You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bazel与Starlark是否支持类似Python*、JS...的展开运算符?

Does Starlark (Bazel) support list unpacking operators like Python's * or JavaScript's ...?

Short answer: No, Starlark (the language used in Bazel build files) does not have a dedicated list unpacking operator equivalent to Python's * or JavaScript's ... for expanding a list into another list.

Alternative 1: Use the + operator for list concatenation

The most common and concise way to achieve your desired behavior is to use the + operator to combine your static list with the returned list from get_more_files(). This works directly in the rule's parameter definition:

some_rule(
    name = "target_name",
    src = ["file1.txt", "file2.txt"] + get_more_files(),
)

This creates a new list that includes all elements from your static files followed by every element returned by get_more_files()—exactly the unpack-and-combine effect you're looking for.

Alternative 2: Use the extend() method

As you noted, you're already aware of the extend() method. This modifies a list in-place by appending all elements from another list to it. Here's how to implement it:

# Start with your base files
srcs = ["file1.txt", "file2.txt"]
# Add all elements from get_more_files() to the list
srcs.extend(get_more_files())

some_rule(
    name = "target_name",
    src = srcs,
)

This approach is handy if you need to build your list incrementally (e.g., adding elements from multiple functions or sources) before passing it to the rule.

Quick note on function argument unpacking

It's also worth mentioning that Starlark doesn't support unpacking lists into function arguments like Python's func(*args) syntax either. If you need to pass multiple arguments from a list to a function, you'll have to access individual elements directly or use helper functions if the target function accepts a list parameter.

内容的提问来源于stack exchange,提问作者Erran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 09:58:11