bazelbuild / starlark

Starlark Language
Apache License 2.0
2.44k stars 163 forks source link

Document starred expressions (aka *args and **kwargs unpack operators) #274

Closed bartekpacia closed 6 months ago

bartekpacia commented 6 months ago

Description

I could not find information about starred expressions neither in Java spec nor in Go spec.

I propose this behavior to be documented in both specs.

Code snippet

def func(arg, *args, **kwargs):
    print("arg: %s, type: %s" % (arg, type(arg)))
    print("args: %s, type: %s" % (args, type(args)))
    print("kwargs: %s, type: %s" % (kwargs, type(kwargs)))

arg = "a"
args = ["b", "c"]
kwargs = {"d": 4, "e": 5}

print("\n--- NOT using starred expressions ---\n")
func(arg, args, kwargs)

print("\n--- Using starred expressions ---\n")
func(arg, *args, **kwargs)

Behavior

$ starlark main.star

--- NOT using starred expressions ---

arg: a, type: string
args: (["b", "c"], {"d": 4, "e": 5}), type: tuple
kwargs: {}, type: dict

--- Using starred expressions ---

arg: a, type: string
args: ("b", "c"), type: tuple
kwargs: {"d": 4, "e": 5}, type: dict
bartekpacia commented 6 months ago

I didn't search well enough – it is documented:

Screenshot 2024-03-26 at 18 00 49