function

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 5 Imported by: 0

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 表示函数接口

func NewNativeFunction

func NewNativeFunction(name string, signature *Signature, fn func(args []interface{}) (interface{}, error)) Function

NewNativeFunction 创建一个原生函数

type NativeFunction

type NativeFunction struct {
	// contains filtered or unexported fields
}

NativeFunction 表示原生 Go 函数

func (*NativeFunction) Execute

func (f *NativeFunction) Execute(args []interface{}) (interface{}, error)

Execute 执行函数

func (*NativeFunction) Name

func (f *NativeFunction) Name() string

Name 返回函数名称

func (*NativeFunction) Signature

func (f *NativeFunction) Signature() *Signature

Signature 返回函数签名

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 函数注册表接口

func NewRegistry

func NewRegistry() Registry

NewRegistry 创建一个新的函数注册表

type Signature

type Signature struct {
	// Parameters 参数类型列表
	Parameters []types.Type

	// ReturnType 返回类型
	ReturnType types.Type

	// Variadic 是否是可变参数函数
	Variadic bool

	// MinArgs 最小参数数量(用于可变参数函数)
	MinArgs int
}

Signature 表示函数签名

func NewSignature

func NewSignature(parameters []types.Type, returnType types.Type) *Signature

NewSignature 创建一个新的函数签名

func NewVariadicSignature

func NewVariadicSignature(minArgs int, returnType types.Type) *Signature

NewVariadicSignature 创建一个可变参数函数签名

func (*Signature) ParameterCount

func (s *Signature) ParameterCount() int

ParameterCount 返回参数数量

func (*Signature) String

func (s *Signature) String() string

String 返回签名的字符串表示

func (*Signature) Validate

func (s *Signature) Validate(args []interface{}) error

Validate 验证参数是否符合签名

Directories

Path Synopsis
Package builtin 提供内置函数库
Package builtin 提供内置函数库

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL