lang: interfaces, funcs: Implement fmt.Stringer for functions

This adds the requirement that all function implementations provider a
String() string method so that these can be used as vertices in the
pgraph library. If we eventually move to generics for the pgraph DAG,
then this might not matter, but it's not bad that these have names
either.
This commit is contained in:
James Shubin
2023-03-03 14:12:09 -05:00
parent 8366cf0873
commit 5d664855de
35 changed files with 365 additions and 39 deletions

View File

@@ -90,7 +90,7 @@ func Register(name string, fns []*types.FuncValue) {
RegisteredFuncs[name] = fns // store a copy for ourselves
// register a copy in the main function database
funcs.Register(name, func() interfaces.Func { return &WrappedFunc{Fns: fns} })
funcs.Register(name, func() interfaces.Func { return &WrappedFunc{Name: name, Fns: fns} })
}
// ModuleRegister is exactly like Register, except that it registers within a
@@ -131,6 +131,8 @@ func consistentArgs(fns []*types.FuncValue) ([]string, error) {
// for the function API, but that can run a very simple, static, pure,
// polymorphic function.
type WrappedFunc struct {
Name string
Fns []*types.FuncValue // list of possible functions
fn *types.FuncValue // the concrete version of our chosen function
@@ -143,6 +145,12 @@ type WrappedFunc struct {
closeChan chan struct{}
}
// String returns a simple name for this function. This is needed so this struct
// can satisfy the pgraph.Vertex interface.
func (obj *WrappedFunc) String() string {
return obj.Name
}
// ArgGen returns the Nth arg name for this function.
func (obj *WrappedFunc) ArgGen(index int) (string, error) {
seq, err := consistentArgs(obj.Fns)