Documentation
¶
Overview ¶
Package function 提供函数注册和管理功能
函数系统支持:
- 函数注册和查找
- 函数签名验证
- 参数类型检查
- 可变参数函数
- 原生 Go 函数包装
示例:
// 创建函数注册表
registry := function.NewRegistry()
// 注册函数
signature := function.NewSignature(
[]types.Type{types.NewNumberType(), types.NewNumberType()},
types.NewNumberType(),
)
registry.RegisterNative("add", signature, func(args []interface{}) (interface{}, error) {
a := args[0].(float64)
b := args[1].(float64)
return a + b, nil
})
// 调用函数
result, _ := registry.Call("add", []interface{}{1.0, 2.0})
// result = 3.0
Package function 提供函数注册和管理功能
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Function ¶
type Function interface {
// Name 返回函数名称
Name() string
// Signature 返回函数签名
Signature() *Signature
// Execute 执行函数
Execute(args []interface{}) (interface{}, error)
}
Function 表示函数接口
type NativeFunction ¶
type NativeFunction struct {
// contains filtered or unexported fields
}
NativeFunction 表示原生 Go 函数
func (*NativeFunction) Execute ¶
func (f *NativeFunction) Execute(args []interface{}) (interface{}, error)
Execute 执行函数
type Registry ¶
type Registry interface {
// Register 注册函数
Register(fn Function) error
// RegisterNative 注册原生函数
RegisterNative(name string, signature *Signature, fn func(args []interface{}) (interface{}, error)) error
// Get 获取函数
Get(name string) (Function, error)
// Has 检查函数是否存在
Has(name string) bool
// Call 调用函数
Call(name string, args []interface{}) (interface{}, error)
// List 列出所有函数名
List() []string
// Unregister 注销函数
Unregister(name string) error
}
Registry 函数注册表接口
type Signature ¶
type Signature struct {
// Parameters 参数类型列表
Parameters []types.Type
// ReturnType 返回类型
ReturnType types.Type
// Variadic 是否是可变参数函数
Variadic bool
// MinArgs 最小参数数量(用于可变参数函数)
MinArgs int
}
Signature 表示函数签名
func NewSignature ¶
NewSignature 创建一个新的函数签名
func NewVariadicSignature ¶
NewVariadicSignature 创建一个可变参数函数签名
Click to show internal directories.
Click to hide internal directories.