Skip to content
On this page

Lambdas

The type of functions in SmartPy is sp.lambda_[t1, t2] where t1 is the argument type and t2 the result type.

Lambdas can be defined either using Python's lambda x: ... syntax or, when they have a name, using def f(x): ....

As such, the definition

f = lambda x: x + 1

is equivalent to:

smartpy
def f(x)
    return x + 1

To call a lambda, simply pass it its argument in parentheses. For example, with the above definition:

smartpy
assert f(1) == 2

Effects

Lambdas can be declared to have so-called effects. There are currently two types of effects in SmartPy:

  • The with_storage effect: this allows a lambda to read and/or write a contract's storage.

  • The with_operations effect: this allows a lambda to emit operations.

with_storage

When defining a lambda you can allow it to access the storage, for example:

smartpy
@sp.effects(with_storage="read-write")
def f(x)
    self.data.abc = 42
    return x + 1

with_storage can also be specified as "read-only" in which case access to the storage is read-only.

with_operations

When defining a lambda you can allow it to emit operations, for example:

smartpy
@sp.effects(with_operations=True)
def f(x)
    sp.transfer(...)
    return x + 1