You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Maybe a new attribute type could be added to better support builtin methods and values? It would also be able to support golang native methods by handling the type casting withing the map functions.
typeAttrfunc(vValue) ValuetypeAttrsmap[string]Attr// Keys returns a new sorted slice of d's keys.func (dAttrs) Keys() []string {
names:=make([]string, 0, len(d))
forname:=ranged {
names=append(names, name)
}
sort.Strings(names)
returnnames
}
func (dAttrs) Attr(name) (Value, error) {
ifa:=d[name]; a!=nil {
returna(d), nil
}
returnnil, nil// no such method
}
// A new type Method would need to be added to support the new function signature.typeMethodstruct {
recvValuenamestringfnfunc(*Thread, string, Tuple, []Tuple) (Value, error)
}
The method signature would be changed, dropping the *Builtin arg and replacing it with fnname string like the first arg in the unpack functions. Could avoid the type casting by implementing custom Attr funcs like func(s *Set) Value but then would need to implement custom Attr() and AttrNames() methods.
As an example Set attrs could then be implemented like:
The API exposed by the starlark package is definitely oriented around the needs of an efficient interpreter implementation, rather than convenience of the application that embeds the interpreter. However, it should be possible to define convenience helpers (e.g. using reflection) outside the repository, as the https://github.com/starlight-go/starlight project does, for example. I suggest you start by experimenting with a helper function that meets your needs in your application's repository.
Types that implement HasAttrs need a way to map strings to values or methods that implement Callable. The simple way to do this is with a switch:
starlark-go/starlark/bench_test.go
Lines 83 to 96 in 5411bad
The main issue with switch cases is the need to ensure all switch cases are covered by
AttrNames()
and vice versa.If Attrs just link to methods the implementation can create a map of *Builtins, like how the internal types map to methods:
starlark-go/starlark/library.go
Lines 142 to 144 in 5411bad
This creates a value to a mapping of type:
starlark-go/starlark/value.go
Line 720 in 5411bad
starlark-go/starlark/library.go
Lines 2171 to 2184 in 5411bad
Maybe a new attribute type could be added to better support builtin methods and values? It would also be able to support golang native methods by handling the type casting withing the map functions.
The method signature would be changed, dropping the *Builtin arg and replacing it with
fnname
string like the first arg in the unpack functions. Could avoid the type casting by implementing custom Attr funcs likefunc(s *Set) Value
but then would need to implement custom Attr() and AttrNames() methods.As an example Set attrs could then be implemented like:
The text was updated successfully, but these errors were encountered: