Scribble Functions
How to write User defined functions
User-defined scribble functions are useful to avoid repeating similar expressions in multiple annotations. They are defined in the docstring above a smart contract using annotations structured as shown below:
The above annotation defines a new user-defined function with name funName
that takes several arguments (arg1
of type type1
, arg2
of type type2
, etc.), and has as its body a single valid Scribble expression (which is also its return value). That expression must be of type returnType
.
Consider the following example. We can see that the operations (x + 10)
and (x + 10) * delta
are repeated multiple times in annotations.
We can avoid those repetitions by adding user defined functions for (x+10)
and (x+10) * delta
above the smart contract. These functions can then be called by annotations inside the contract.
In summary there are several important functions to remember about scribble user-defined functions:
They are defined in the docstring above a smart contract
They can be used by any annotation inside that contract, or in all inheriting contracts
Scribble user defined functions must be
view
orpure
functions. I.e. they cannot modify any state, but can still access the state variables as shown in the above example.The body of a user-defined function consists of a single valid Scribble expression, which is also the return value of the function
User defined functions can also be used by all inheriting contracts.
Last updated