Skip to main content
In Baretta, a list holds values of one type, and a dict has one key type and one value type. You can create them automatically or manually.

Automatic

In normal Python, you can mix types freely:
In Baretta, the translator reads the first element and sets the type from it. Any element with a different type is rejected.

Manual

You can also set the types yourself.
Lists work the same way:

Reading values

A key (or an index) might not exist, so Baretta gives you three ways to read a value. Pick the one that matches what you want to happen when it is missing. No style ever returns None. The only ways a read can fail are hello[key], which is fatal, and find, which you handle yourself.

Index: hello[key]

Use this when the key must exist.
A missing key stops the program. On a node, the whole transaction is reverted. In the terminal, Baretta prints the reason and exits with an error code.

get with a default

Use this when you always want a value.
The compiler checks the default against the value type of the dict, so a wrong default is rejected before the program runs:
An integer literal takes the value type of the dict, so you do not need to write the type. The default must be a literal or a constant.

get without a default

If you leave the default out, Baretta uses the default value of the type. The compiler decides this, so you never have to write it.
Some types have no safe default, for example an address or a type you define yourself. For these types the compiler rejects get(key) and asks you to give a default or use find. A silent “zero” is a dangerous value for such types.
This is different from Python, where dict.get(key) returns None when the key is missing. Baretta has no None value, so it returns the default of the type instead.

find

Use find when you want to handle the missing key yourself. It returns a Lookup.

Lists

Lists work the same way. A missing value means the index is out of range.

Which style should I use?

See Lookup for the methods of find and for functions that can fail.