simple

package
v0.0.0-...-1f90de3 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: GPL-3.0 Imports: 11 Imported by: 40

Documentation

Index

Constants

View Source
const (
	// DirectInterface specifies whether we should use the direct function
	// API or not. If we don't use it, then these simple functions are
	// wrapped with the struct below.
	DirectInterface = false // XXX: fix any bugs and set to true!
)

Variables

View Source
var RegisteredFuncs = make(map[string]*Scaffold) // must initialize

RegisteredFuncs maps a function name to the corresponding function scaffold.

Functions

func ModuleRegister

func ModuleRegister(module, name string, scaffold *Scaffold)

ModuleRegister is exactly like Register, except that it registers within a named module. This is a helper function.

func Register

func Register(name string, scaffold *Scaffold)

Register registers a simple, static, pure, polymorphic function. It is easier to use than the raw function API. It allows you to build and check a function based on a type signature that contains unification variables. You may only specify a single type signature with the API, so some complex patterns are not possible with this API. Implementing a function like `printf` would not be possible. Implementing a function which counts the number of elements in a list would be.

func StructRegister

func StructRegister(moduleName string, args interface{}) error

StructRegister takes an CLI args struct with optional struct tags, and generates simple functions from the contained fields in the specified namespace. If no struct field named `func` is included, then a default function name which is the lower case representation of the field name will be used, otherwise the struct tag contents are used. If the struct tag contains the `-` character, then the field will be skipped. TODO: An alternative version of this might choose to return all of the values as a single giant struct.

func TypeMatch

func TypeMatch(typeList []string) func(*types.Type) error

TypeMatch accepts a list of possible type signatures that we want to check against after type unification. This helper function returns a function which is suitable for use in the scaffold check function field.

Types

type Func

type Func struct {
	*docsUtil.Metadata
	*WrappedFunc // *wrapped.Func as a type alias to pull in the base impl.

	// Check is a check function to run after type unification. It will get
	// passed the solved type of this function. It should error if this is
	// not an acceptable option. This function can be omitted.
	Check func(typ *types.Type) error

	// Func is the implementation of the function. The input type can be
	// determined by inspecting the values. Of note, this API does not tell
	// the implementation what the correct return type should be. If it
	// can't be determined from the input types, then a different function
	// API needs to be used. XXX: Should we extend this here?
	Func interfaces.FuncSig
}

Func is a scaffolding function struct which fulfills the boiler-plate for the function API, but that can run a very simple, static, pure, polymorphic function.

func (*Func) Build

func (obj *Func) Build(typ *types.Type) (*types.Type, error)

Build is run to turn the maybe polymorphic, undetermined function, into the specific statically typed version. It is usually run after unification completes, and must be run before Info() and any of the other Func interface methods are used. For this function API, it just runs the Check function to make sure that the type found during unification is one of the valid ones.

type Scaffold

type Scaffold struct {
	// T is the type of the function. It can include unification variables.
	// At a minimum, this must be a `func(?1) ?2` as a naked `?1` is not
	// allowed. (TODO: Because of ArgGen.)
	T *types.Type

	// C is a check function to run after type unification. It will get
	// passed the solved type of this function. It should error if this is
	// not an acceptable option. This function can be omitted.
	C func(typ *types.Type) error

	// F is the implementation of the function. The input type can be
	// determined by inspecting the values. Of note, this API does not tell
	// the implementation what the correct return type should be. If it
	// can't be determined from the input types, then a different function
	// API needs to be used. XXX: Should we extend this here?
	F interfaces.FuncSig

	// D is the documentation handle for this function. We look on that
	// struct or function for the doc string instead of the F field if this
	// is specified. (This is used for facts.)
	D interface{}
}

Scaffold holds the necessary data to build a (possibly polymorphic) function with this API.

type WrappedFunc

type WrappedFunc = wrapped.Func

WrappedFunc is a type alias so that we can embed `wrapped.Func` inside our struct, since the Func name collides with our Func field name.

Jump to

Keyboard shortcuts

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