Stateful
Stateful
is a useful tool for managing property states.
Data Types
public defstruct Stateful<K,V> :
current: K|False
states: Tuple<KeyValue<K,V>>
public defn Stateful<?K,?V> (states:Seqable<KeyValue<?K, ?V>>) -> Stateful
public defn Stateful<?K,?V> (current:JITXValue, states:Seqable<KeyValue<?K, ?V>>) -> Stateful
Stateful
is a collection of states with a possibly current state.
current
: the current state, or false if there is no current state.states
: mappings from a state's name to a value.
Function
public defn get<?K,?V> (s:Stateful<?K,?V>, state:?K) -> V :
Returns the value for the given state.
public defn state-names<?K,?V> (s:Stateful<?K,?V>) -> Tuple<K> :
Returns the names of all states.
Additional Syntaxes
state(<object>.<property>) = <state>
Set the property's state to state
. It is required that object.property
is a Stateful. This statement must be declared inside a pcb-module, pcb-component, or support option.
state(<object>.<property>)
Get the property's current state. It is required that both object.property
is a Stateful and there is a current state.
state?(<object>.<property>)
Get the property's current state. It is required that object.property
is a Stateful. Returns false if there is no current state.
has-state?(<object>.<property>, <state>)
Returns whether state
is one of object.property
's states. It is required that object.property
is a Stateful.
pcb-module :
port p : pin[5]
;Assign a Stateful property to pin p[3] in a USB-powered system.
;The low state load-current is 100mA and the high state load-current is 1.5A.
;No initial state is assigned.
property(p[3].load-current) = Stateful(["low" => 100.0e-3,
"high" => 1.5])
;Try getting the current state. Returns false.
state?(p[3].load-current)
;Set the state to "low".
;Much like properties, states can be set inside a module, component, or support option.
state(p[3].load-current) = "low"
;Get the current state. Returns "low".
state(p[3].load-current)
;Does the property have a "high" state? Returns true.
has-state?(p[3].load-current, "high")
The above pcb-module demonstrates how all syntaxes can be used.