Tuples and records
Pairs
(a1, a2)
denotes a pair in SmartPy. If a1
is of type t1
and a2
is of type t2
, then (a1, a2)
is of type sp.pair[t1, t2]
. For example, (42, "abc")
is of type sp.pair[sp.int, sp.string]
.
- sp.fst(sp.pair[t1, t2]) → t1
Extracts the first component of a pair. For example,
sp.fst((42, "abc")) == 42
.
- sp.snd(sp.pair[t1, t2]) → t2
Extracts the second component of a pair. For example,
sp.snd((42, "abc")) == "abc"
.
Tuples
More generally, SmartPy supports tuples with arbitrary many components. For example, (42, "abc", True)
is of type sp.tuple[sp.int, sp.string, sp.bool]
.
Tuples can be matched on the left-hand side of assignments, for example:
smartpy
(a, b, c) = (42, "abc", True)
assert a == 42
assert b == "abc"
assert c == True
Records
Records are written as sp.record(a=42, b="abc")
, which is of type sp.record(a=sp.int, b=sp.string
. A record's components can be accessed with .
-notation, for example:
smartpy
x = sp.record(a=42, b="abc", c=True)
assert x.a == 42
assert x.b == "abc"
assert x.c == True