Documentation
¶
Overview ¶
Package vals contains basic facilities for manipulating values used in the Elvish runtime.
Index ¶
- Variables
- func Assoc(a, k, v any) (any, error)
- func Bool(v any) bool
- func CanIterate(v any) bool
- func Collect(it any) ([]any, error)
- func Concat(lhs, rhs any) (any, error)
- func ConvertToFloat64(num Num) float64
- func Dissoc(a, k any) any
- func Equal(x, y any) bool
- func Feed(f func(any) bool, values ...any)
- func FromGo(a any) any
- func HasKey(container, key any) bool
- func Hash(v any) uint32
- func Index(a, k any) (any, error)
- func Iterate(v any, f func(any) bool) error
- func IterateKeys(v any, f func(any) bool) error
- func Kind(v any) string
- func Len(v any) int
- func MakeList(vs ...any) vector.Vector
- func MakeListSlice[T any](vs []T) vector.Vector
- func MakeMap(a ...any) hashmap.Map
- func NoSuchKey(k any) error
- func Optional(ptr any) any
- func PromoteToBigInt(n Num) *big.Int
- func PromoteToBigRat(n Num) *big.Rat
- func Repr(v any, indent int) string
- func ReprPlain(v any) string
- func ScanListElementsToGo(src List, ptrs ...any) error
- func ScanListToGo(src List, ptr any) error
- func ScanMapToGo(src Map, ptr any) error
- func ScanToGo(src any, ptr any) error
- func StructFieldsInfo(t reflect.Type) ([]string, map[string]int)
- func ToString(v any) string
- func TypeOf(i any) reflect.Type
- func ValueOf(i any) reflect.Value
- type Assocer
- type Booler
- type Concatter
- type Dissocer
- type Equaler
- type ErrIndexer
- type File
- type HasKeyer
- type Hasher
- type Indexer
- type Iterator
- type KeysIterator
- type Kinder
- type Lener
- type List
- type ListIndex
- type ListReprBuilder
- type Map
- type MapReprBuilder
- type Num
- type NumSlice
- type NumType
- type Pipe
- type PseudoStructMap
- type RConcatter
- type Reprer
- type Stringer
- type StructMap
- type Tester
- func (vt Tester) AllKeys(wantKeys ...any) Tester
- func (vt Tester) Assoc(key, val, wantNew any) Tester
- func (vt Tester) AssocError(key, val any, wantErr error) Tester
- func (vt Tester) Bool(wantBool bool) Tester
- func (vt Tester) Equal(others ...any) Tester
- func (vt Tester) HasKey(keys ...any) Tester
- func (vt Tester) HasNoKey(keys ...any) Tester
- func (vt Tester) Hash(wantHash uint32) Tester
- func (vt Tester) Index(key, wantVal any) Tester
- func (vt Tester) IndexError(key any, wantErr error) Tester
- func (vt Tester) Kind(wantKind string) Tester
- func (vt Tester) Len(wantLen int) Tester
- func (vt Tester) NotEqual(others ...any) Tester
- func (vt Tester) Repr(wantRepr string) Tester
- type WrongType
Constants ¶
This section is empty.
Variables ¶
var CallableType reflect.Type
CallableType should be set from the eval package to the type of eval.Callable. It is not initialized here to avoid creating an import cycle.
var EmptyList = vector.Empty
EmptyList is an empty list.
var EmptyMap = hashmap.New(Equal, Hash)
EmptyMap is an empty map.
var ErrConcatNotImplemented = errors.New("concat not implemented")
ErrConcatNotImplemented is a special error value used to signal that concatenation is not implemented. See Concat for how it is used.
Functions ¶
func Assoc ¶
Assoc takes a container, a key and value, and returns a modified version of the container, in which the key associated with the value. It is implemented for the builtin type string, List and Map types, StructMap types, and types satisfying the Assocer interface. For other types, it returns an error.
func Bool ¶
Bool converts a value to bool. It is implemented for nil, the builtin bool type, and types implementing the Booler interface. For all other values, it returns true.
func CanIterate ¶
CanIterate returns whether the value can be iterated. If CanIterate(v) is true, calling Iterate(v, f) will not result in an error.
func Concat ¶
Concat concatenates two values. If both operands are strings, it returns lhs + rhs, nil. If the left operand implements Concatter, it calls lhs.Concat(rhs). If lhs doesn't implement the interface or returned ErrConcatNotImplemented, it then calls rhs.RConcat(lhs). If all attempts fail, it returns nil and an error.
func ConvertToFloat64 ¶
ConvertToFloat64 converts any number to float64. It panics if num is not a number value.
func Dissoc ¶
Dissoc takes a container and a key, and returns a modified version of the container, with the given key dissociated with any value. It is implemented for the Map type and types satisfying the Dissocer interface. For other types, it returns nil.
func Equal ¶
Equal returns whether two values are equal. It is implemented for the builtin types bool and string, the File, List, Map types, StructMap types, and types satisfying the Equaler interface. For other types, it uses reflect.DeepEqual to compare the two values.
func Feed ¶
Feed calls the function with given values, breaking earlier if the function returns false.
func FromGo ¶
FromGo converts a Go value to an Elvish value.
Exact numbers are normalized to the smallest types that can hold them, and runes are converted to strings. Values of other types are returned unchanged.
func HasKey ¶
HasKey returns whether a container has a key. It is implemented for the Map type, StructMap types, and types satisfying the HasKeyer interface. It falls back to iterating keys using IterateKeys, and if that fails, it falls back to calling Len and checking if key is a valid numeric or slice index. Otherwise it returns false.
func Hash ¶
Hash returns the 32-bit hash of a value. It is implemented for the builtin types bool and string, the File, List, Map types, StructMap types, and types satisfying the Hasher interface. For other values, it returns 0 (which is OK in terms of correctness).
func Index ¶
Index indexes a value with the given key. It is implemented for the builtin type string, *os.File, List, StructMap and PseudoStructMap types, and types satisfying the ErrIndexer or Indexer interface (the Map type satisfies Indexer). For other types, it returns a nil value and a non-nil error.
func Iterate ¶
Iterate iterates the supplied value, and calls the supplied function in each of its elements. The function can return false to break the iteration. It is implemented for the builtin type string, the List type, and types satisfying the Iterator interface. For these types, it always returns a nil error. For other types, it doesn't do anything and returns an error.
func IterateKeys ¶
IterateKeys iterates the keys of the supplied value, calling the supplied function for each key. The function can return false to break the iteration. It is implemented for the Map type, StructMap types, and types satisfying the IterateKeyser interface. For these types, it always returns a nil error. For other types, it doesn't do anything and returns an error.
func Kind ¶
Kind returns the "kind" of the value, a concept similar to type but not yet very well defined. It is implemented for the builtin nil, bool and string, the File, List, Map types, StructMap types, and types satisfying the Kinder interface. For other types, it returns the Go type name of the argument preceded by "!!".
TODO: Decide what `kind-of` should report for an external command object and document the rationale for the choice in the doc string for `func (ExternalCmd) Kind()` as well as user facing documentation. It's not obvious why this returns "fn" rather than "external" for that case.
func Len ¶
Len returns the length of the value, or -1 if the value does not have a well-defined length. It is implemented for the builtin type string, StructMap types, and types satisfying the Lener interface. For other types, it returns -1.
func MakeListSlice ¶ added in v0.19.0
MakeListSlice creates a new List from a slice.
func MakeMap ¶
MakeMap creates a map from arguments that are alternately keys and values. It panics if the number of arguments is odd.
func Optional ¶ added in v0.18.0
Optional wraps the last pointer passed to ScanListElementsToGo, to indicate that it is optional.
func PromoteToBigInt ¶
PromoteToBigInt converts an int or *big.Int to a *big.Int. It panics if n is any other type.
func PromoteToBigRat ¶
PromoteToBigRat converts an int, *big.Int or *big.Rat to a *big.Rat. It panics if n is any other type.
func Repr ¶
Repr returns the representation for a value, a string that is preferably (but not necessarily) an Elvish expression that evaluates to the argument. The representation is pretty-printed, using indent as the initial level of indentation. It is implemented for the builtin types nil, bool and string, the File, List and Map types, StructMap types, and types satisfying the Reprer interface. For other types, it uses fmt.Sprint with the format "<unknown %v>".
func ScanListElementsToGo ¶ added in v0.18.0
ScanListElementsToGo unpacks elements from a list, storing the each element in the given pointers with ScanToGo.
The last pointer may be wrapped with Optional to indicate that it is optional.
func ScanListToGo ¶ added in v0.18.0
ScanListToGo converts a List to a slice, using ScanToGo to convert each element.
func ScanMapToGo ¶ added in v0.18.0
ScanMapToGo scans map elements into ptr, which must be a pointer to a struct. Struct field names are converted to map keys with CamelToDashed.
The map may contains keys that don't correspond to struct fields, and it doesn't have to contain all keys that correspond to struct fields.
func ScanToGo ¶
ScanToGo converts an Elvish value, and stores it in the destination of ptr, which must be a pointer.
If ptr has type *int, *float64, *Num or *rune, it performs a suitable conversion, and returns an error if the conversion fails. In other cases, this function just tries to perform "*ptr = src" via reflection and returns an error if the assignment can't be done.
func StructFieldsInfo ¶ added in v0.18.0
StructFieldsInfo takes a type for a struct, and returns a slice for each field name, converted with CamelToDashed, and a reverse index. Unexported fields result in an empty string in the slice, and is omitted from the reverse index.
func ToString ¶
ToString converts a Value to string. It is implemented for the builtin float64 and string types, and type satisfying the Stringer interface. It falls back to Repr(v).
Types ¶
type Assocer ¶
type Assocer interface { // Assoc returns a slightly modified version of the receiver with key k // associated with value v. Assoc(k, v any) (any, error) }
Assocer wraps the Assoc method.
type Booler ¶
type Booler interface { // Bool computes the truth value of the receiver. Bool() bool }
Booler wraps the Bool method.
type Concatter ¶
type Concatter interface { // Concat concatenates the receiver with another value, the receiver being // the left operand. If concatenation is not supported for the given value, // the method can return the special error type ErrCatNotImplemented. Concat(v any) (any, error) }
Concatter wraps the Concat method. See Concat for how it is used.
type Dissocer ¶
type Dissocer interface { // Dissoc returns a slightly modified version of the receiver with key k // dissociated with any value. Dissoc(k any) any }
Dissocer wraps the Dissoc method.
type Equaler ¶
type Equaler interface { // Equal compares the receiver to another value. Two equal values must have // the same hash code. Equal(other any) bool }
Equaler wraps the Equal method.
type ErrIndexer ¶
type ErrIndexer interface { // Index retrieves one value from the receiver at the specified index. Index(k any) (any, error) }
ErrIndexer wraps the Index method.
type HasKeyer ¶
type HasKeyer interface { // HasKey returns whether the receiver has the given argument as a valid // key. HasKey(any) bool }
HasKeyer wraps the HasKey method.
type Hasher ¶
type Hasher interface { // Hash computes the hash code of the receiver. Hash() uint32 }
Hasher wraps the Hash method.
type Indexer ¶
type Indexer interface { // Index retrieves the value corresponding to the specified key in the // container. It returns the value (if any), and whether it actually exists. Index(k any) (v any, ok bool) }
Indexer wraps the Index method.
type Iterator ¶
type Iterator interface { // Iterate calls the passed function with each value within the receiver. // The iteration is aborted if the function returns false. Iterate(func(v any) bool) }
Iterator wraps the Iterate method.
type KeysIterator ¶
type KeysIterator interface { // IterateKeys calls the passed function with each key within the receiver. // The iteration is aborted if the function returns false. IterateKeys(func(v any) bool) }
KeysIterator wraps the IterateKeys method.
type Lener ¶
type Lener interface { // Len computes the length of the receiver. Len() int }
Lener wraps the Len method.
type ListReprBuilder ¶
type ListReprBuilder struct {
// contains filtered or unexported fields
}
ListReprBuilder helps to build Repr of list-like Values.
func NewListReprBuilder ¶
func NewListReprBuilder(indent int) *ListReprBuilder
NewListReprBuilder makes a new ListReprBuilder.
func (*ListReprBuilder) String ¶
func (b *ListReprBuilder) String() string
String returns the representation that has been built. After it is called, the ListReprBuilder may no longer be used.
func (*ListReprBuilder) WriteElem ¶
func (b *ListReprBuilder) WriteElem(v string)
WriteElem writes a new element.
type MapReprBuilder ¶
type MapReprBuilder struct {
// contains filtered or unexported fields
}
MapReprBuilder helps building the Repr of a Map. It is also useful for implementing other Map-like values. The zero value of a MapReprBuilder is ready to use.
func NewMapReprBuilder ¶
func NewMapReprBuilder(indent int) *MapReprBuilder
NewMapReprBuilder makes a new MapReprBuilder.
func (*MapReprBuilder) String ¶
func (b *MapReprBuilder) String() string
String returns the representation that has been built. After it is called, the MapReprBuilder should no longer be used.
type Num ¶
type Num any
Num is a stand-in type for int, *big.Int, *big.Rat or float64. This type doesn't offer type safety, but is useful as a marker; for example, it is respected when parsing function arguments.
func NormalizeBigInt ¶
NormalizeBigInt converts a big.Int to an int if it is within the range of int. Otherwise it returns n as is.
func NormalizeBigRat ¶
NormalizeBigRat converts a big.Rat to a big.Int (or an int if within the range) if its denominator is 1.
func ParseNum ¶
ParseNum parses a string into a suitable number type. If the string does not represent a valid number, it returns nil.
func UnifyNums2 ¶
UnifyNums2 is like UnifyNums, but is optimized for two numbers.
type NumSlice ¶
type NumSlice any
NumSlice is a stand-in type for []int, []*big.Int, []*big.Rat or []float64. This type doesn't offer type safety, but is useful as a marker.
type Pipe ¶
Pipe wraps a pair of pointers to os.File that are the two ends of the same pipe.
type PseudoStructMap ¶
type PseudoStructMap interface{ Fields() StructMap }
PseudoStructMap may be implemented by a type to derive the Repr, Index, HasKey and IterateKeys operations from the struct map returned by the Fields method.
type RConcatter ¶
RConcatter wraps the RConcat method. See Concat for how it is used.
type Reprer ¶
type Reprer interface { // Repr returns a string that represents a Value. The string either be a // literal of that Value that is preferably deep-equal to it (like `[a b c]` // for a list), or a string enclosed in "<>" containing the kind and // identity of the Value(like `<fn 0xdeadcafe>`). // // If indent is at least 0, it should be pretty-printed with the current // indentation level of indent; the indent of the first line has already // been written and shall not be written in Repr. The returned string // should never contain a trailing newline. Repr(indent int) string }
Reprer wraps the Repr method.
type Stringer ¶
type Stringer interface { // Stringer converts the receiver to a string. String() string }
Stringer wraps the String method.
type StructMap ¶
type StructMap interface{ IsStructMap() }
StructMap may be implemented by a struct to mark the struct as a "struct map", which causes Elvish to treat it like a read-only map. Each exported, named field and getter method (a method taking no argument and returning one value) becomes a field of the map, with the name mapped to dash-case.
The following operations are derived for structmaps: Kind, Repr, Hash, Len, Index, HasKey and IterateKeys.
Example:
type someStruct struct { FooBar int lorem string } func (someStruct) IsStructMap() { } func (s SomeStruct) Ipsum() string { return s.lorem } func (s SomeStruct) OtherMethod(int) { }
An instance of someStruct behaves like a read-only map with 3 fields: foo-bar, lorem and ipsum.
type Tester ¶ added in v0.18.0
type Tester struct {
// contains filtered or unexported fields
}
Tester is a helper for testing properties of a value.
func (Tester) AllKeys ¶ added in v0.18.0
AllKeys tests that the given keys match what the result of IterateKeys on the value.
NOTE: This now checks equality using reflect.DeepEqual, since all the builtin types have string keys. This can be changed in future to use Equal is the need arises.
func (Tester) Assoc ¶ added in v0.18.0
Assoc tests that Assoc'ing the value with the given key-value pair returns the wanted new value and no error.
func (Tester) AssocError ¶ added in v0.18.0
AssocError tests that Assoc'ing the value with the given key-value pair returns the given error.
func (Tester) Equal ¶ added in v0.18.0
Equal tests that the value is Equal to every of the given values.
func (Tester) HasNoKey ¶ added in v0.18.0
HasNoKey tests that the value does not have any of the given keys.
func (Tester) Index ¶ added in v0.18.0
Index tests that Index'ing the value with the given key returns the wanted value and no error.
func (Tester) IndexError ¶ added in v0.18.0
IndexError tests that Index'ing the value with the given key returns the given error.