Documentation ¶
Overview ¶
Package typed contains implementation of strongly-typed lists and dicts.
Index ¶
- type Converter
- type Dict
- func (d *Dict) Attr(name string) (starlark.Value, error)
- func (d *Dict) AttrNames() []string
- func (d *Dict) Clear() error
- func (d *Dict) CompareSameType(op syntax.Token, y starlark.Value, depth int) (bool, error)
- func (d *Dict) Delete(k starlark.Value) (starlark.Value, bool, error)
- func (d *Dict) Freeze()
- func (d *Dict) Get(k starlark.Value) (v starlark.Value, found bool, err error)
- func (d *Dict) Hash() (uint32, error)
- func (d *Dict) Items() []starlark.Tuple
- func (d *Dict) Iterate() starlark.Iterator
- func (d *Dict) KeyConverter() Converter
- func (d *Dict) Keys() []starlark.Value
- func (d *Dict) Len() int
- func (d *Dict) SetKey(k, v starlark.Value) error
- func (d *Dict) String() string
- func (d *Dict) Truth() starlark.Bool
- func (d *Dict) Type() string
- func (d *Dict) ValueConverter() Converter
- type List
- func (l *List) Append(v starlark.Value) error
- func (l *List) Attr(name string) (starlark.Value, error)
- func (l *List) AttrNames() []string
- func (l *List) Binary(op syntax.Token, y starlark.Value, side starlark.Side) (starlark.Value, error)
- func (l *List) Clear() error
- func (l *List) CompareSameType(op syntax.Token, y starlark.Value, depth int) (bool, error)
- func (l *List) Converter() Converter
- func (l *List) Freeze()
- func (l *List) Hash() (uint32, error)
- func (l *List) Index(i int) starlark.Value
- func (l *List) Iterate() starlark.Iterator
- func (l *List) Len() int
- func (l *List) SetIndex(i int, v starlark.Value) error
- func (l *List) Slice(start, end, step int) starlark.Value
- func (l *List) String() string
- func (l *List) Truth() starlark.Bool
- func (l *List) Type() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Converter ¶
type Converter interface { // Convert takes a value and either returns it as is (if it already has the // necessary type) or allocates a new value of necessary type and populates // it based on data in 'x'. // // Returns an error if 'x' can't be converted. Convert(x starlark.Value) (starlark.Value, error) // Type returns a name of the type the converter converts to. // // Used only to construct composite type names such as "list<T>". Type() string }
Converter can convert values to a some Starlark type or reject them as incompatible.
Must be idempotent, i.e. the following must not panic for all 'x':
y, err := Convert(x) if err == nil { z, err := Convert(y) if err != nil { panic("converted to an incompatible type") } if z != y { panic("doesn't pass through already converted item") } }
Must be stateless. Must not mutate the values being converted.
type Dict ¶
type Dict struct {
// contains filtered or unexported fields
}
Dict is a Starlark value that looks like a regular dict, but implements type checks and implicit conversions for keys and values when assigning elements.
Note that keys(), values() and items() methods return regular lists, not typed ones.
Differences from regular dicts:
- Not comparable by value to regular dicts, only to other typed dicts. go.starlark.net has no way to express that typed dicts should be comparable to regular dicts.
- update() doesn't support keyword arguments. Using keyword arguments implies string keys which make no sense in generic typed Dict interface, since generally strings are not valid keys. This also substantially simplifies the implementation of update().
func AsTypedDict ¶
AsTypedDict allocates a new dict<k,v>, and copies it from 'x'.
Returns an error if 'x' is not an iterable mapping or some of its (k, v) pairs can't be converted to requested types.
func (*Dict) CompareSameType ¶
func (*Dict) KeyConverter ¶
KeyConverter returns the type converter used for keys of this dict.
func (*Dict) ValueConverter ¶
ValueConverter returns the type converter used for values of this dict.
type List ¶
type List struct {
// contains filtered or unexported fields
}
List is a Starlark value that looks like a regular list, but implements type checks and implicit conversions when assigning elements.
Differences from regular lists:
- Not comparable by value to regular lists, only to other typed lists. go.starlark.net has no way to express that typed lists should be comparable to regular lists.
- Only == and != comparison operators are supported.
- `l += ...` is same as `l = l + ...`, not `l.extend(...)`
func AsTypedList ¶
AsTypedList allocates a new list<t>, and copies it from 'x'.
Returns an error if 'x' is not an iterable or some of its elements can't be converted to 't'.
func NewList ¶
NewList returns a list with given elements, converted to necessary type through 'item' converter.
All subsequently added elements will be converter through this converter too.
func (*List) Binary ¶
func (l *List) Binary(op syntax.Token, y starlark.Value, side starlark.Side) (starlark.Value, error)
Binary implements 'l <op> y' (or 'y <op> l', depending on 'side').
Note that *starlark.List has its binary operators implement natively by the Starlark evaluator, not via HasBinary interface.