Syntax

The etch language was initially integrated with the Fetch.AI C++ ledger project. It is designed to run on a number of platforms.

etch is inspired by Rust, C++, and Python, but it also has some idiosyncrasies.

The etch language is extremely restrictive to avoid costly errors on the ledger. See Smart Contract section for more information.

etch is a statically typed language and static typing is enforced by the compiler.

Note

Blocks of code require no delimiters. White space is ignored. Indentation is recommended.

You can extend and customise etch in C++. We show you how to do that here.

Let's take a closer look at etch.

Declarations

Explicitly declare the type:

function main()

    var myvariable : String;

endfunction

In some cases, type can be inferred:

function main()

    var myvariable = "hello";

endfunction

Assign a value like this:

function main()

    var myvariable : String = "hello";

endfunction

For multivariate types, all the types must be declared:

    var multitype-variable-name : Type<Type<Type>>();

Not declaring the variable type and inferrable value results in a compilation error:

function main()

    var myVariable;

endfunction 

Failed to compile.
line 3: error at ';', expected ':' or '='
Failed to compile.

Main

Initially, all our code snippets execute inside the main function:

function main()

    // stuff

endfunction

Keywords

Here is a current list of etch keywords:

var if endif else
for endfor while endwhile
function endfunction break continue
return

Comments

Both line and block comments are possible:

// a single comment inside a function

/* 
   ..lines of commented out stuff inside and outside functions
*/

Annotations

etch code for smart contracts includes annotated functions. These are more like Java method annotations and not at all like Python decorators:

  • @init is a constructor method that initialises the contract.

  • @action is a function which defines transactions on the ledger that change state.

  • @query is a function that allows you to query data residing on the ledger.

We tell you all about these functions in the section on smart contracts.