google / starlark-go

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

Expose Function defaults. #450

Closed vtsao closed 1 year ago

vtsao commented 1 year ago

Would it be possible to expose Function defaults?

I'm working on an integration where I need to get the default values before executing the function. Exposing it may be as simple as adding a func (fn *Function) Defaults() Tuple function. Not sure if defaults weren't exposed on purpose?

adonovan commented 1 year ago

That tuple is not a valid Starlark value, as it may contain elements of type mandatory, which are not valid values. Even if that were not the case, it seems like the representation should be encapsulated to permit future evolution.

Why do you need to access the defaults tuple? Isn't that a violation of the function abstraction?

vtsao commented 1 year ago

Why do you need to access the defaults tuple?

I'm integrating Starlark functions with a command line tool we own and I want to get the types of the function parameters so I can automatically generate flags for the command. When registering flags, I need to know the type of the flag, like is it a number, list, map, etc. So I was hoping to use param defaults for that.

adonovan commented 1 year ago

Ok, let's add a func (fn *Function) ParamDefault(i int) Value method after Param, that returns nil for mandatory parameters and panics unless 0 <= i < NumParams.

vtsao commented 1 year ago

Thanks, I'll create a PR.