google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

why load statement must import at least 1 symbol #417

Closed vanishs closed 1 year ago

vanishs commented 1 year ago

I made a packager tool: https://github.com/vanishs/starc2one This tool packages the relevant dependent modules through the ”load“ reference I want to package them into a file by referencing them by "load". like this:

load("mod1") load("mod2") load("mod3")

but i got an error: "load statement must import at least 1 symbol"

Why is there such a limit?

laurentlb commented 1 year ago

The point of a load statement is to import a symbol in the environment. So a load with no symbol would be a no-op.

If you need to do a side-effect, you could define your own function, e.g. import("mod1").

vanishs commented 1 year ago

could you add a parameter AllowLoadNothing to give "load" the ability to run a module?

adonovan commented 1 year ago

A Starlark load statement must specify both the name of a module to load, and a list of 1 or more symbols to import from it. Anything else is a syntax error. There is no way to say "import all the symbols from a module into this file", because it would make it very hard to tell where a symbol was defined.

However, there is an open spec proposal for a way to say "import an entire module to the local name foo", so that you could then access members using the syntax foo.x, but this has not been accepted. Personally I like it but it would be a fair amount of work to implement. Feel free to comment on that issue (and see also https://github.com/google/starlark-go/issues/302).

vanishs commented 1 year ago

@adonovan I mean allow the load statement not to import any symbols. like @laurentlb understands.

adonovan commented 1 year ago

What would be the value of a load statement that doesn't load anything?

vanishs commented 1 year ago

Nothing at all. like typescript. you can write like this code: import {} from "path/mymodule1"