This is useful when dealing with json parsed types for example.
Assign is a tool that allows assigning any value to any other value and let the library handle the conversion in a somewhat intelligent way.
For example a map[string]any
can be assigned to a struct (json
tags will be taken into account for variable names) and values will be converted.
It is possible to transform any func into a generic callable that can be used in various ways, with its arguments automatically converted to match the required values.
For example:
func Add(a, b int) int {
return a + b
}
f := typutil.Func(Add)
res, err := typutil.Call[int](f, ctx, 1, "2") // res=3
Arguably, default arguments are something missing with Go. Well, here we go.
func Add(a, b int) int {
return a + b
}
f := typutil.Func(Add).WithDefaults(typutil.Required, 42)
res, err := typutil.Call[int](f, ctx, 58) // res=100