Solidity vs FunC
Smart contract development involves usage of predefined languages such as Solidity for Ethereum, and FunC for TON. Solidity is an object-oriented, high-level, strictly-typed language influenced by C++, Python, and JavaScript, and is specifically designed for writing smart contracts that execute on Ethereum blockchain platforms.
FunC is also a high-level language, used to program smart contracts on TON Blockchain, being a domain-specific, C-like, statically-typed language.
In the sections below will be analyzed briefly the following aspects of these languages, i.e. data types, storage, functions, flow control structures and dictionaries (hashmaps).
Storage layout
Solidity provides a flat storage model, which means that all state variables are stored in a single, continuous block of memory called the storage. The storage is a key-value store where each key is a 256-bit (32-byte) integer that represents the storage slot number, and each value is the 256-bit word stored at that slot. The slots are numbered sequentially starting from zero, and each slot can store a single word. Solidity allows the programmer to specify the storage layout by using the storage keyword to define state variables. The order in which the variables are defined determines their position in the storage.
Permanent storage data in TON Blockchain is stored as a cell. Cells play the role of memory in the stack-based TVM. A cell can be transformed into a slice, and then the data bits and references to other cells from the cell can be obtained by loading them from the slice. Data bits and references to other cells can be stored into a builder, and then the builder can be finalized into a new cell.
Data types
Solidity includes the following basic data types:
- Signed/Unsigned integers
- Boolean
- Addresses – used to store Ethereum wallet or smart contract addresses, typically around 20 bytes. An address type can be suffixed with the keyword “payable”, which restricts it to store only wallet addresses and use the transfer and send crypto functions.
- Byte arrays – declared with the keyword “bytes”, is a fixed-size array used to store a predefined number of bytes up to 32, usually declared along with the keyword.
- Literals – Immutable values such as addresses, rationals and integers, strings, unicode and hexadecimals, which can be stored in a variable.
- Enums
- Arrays (fixed/dynamic)
- Structs
- Mappings
In case of FunC, the main data types are:
- Integers
- Cell – basic for TON opaque data structure, which contains up to 1,023 bits and up to 4 references to other cells
- Slice and Builder – special objects to read from and write to cells,
- Continuation – another flavour of cell that contains ready-to-execute TVM byte-code
- Tuples – is an ordered collection of up to 255 components, having arbitrary value types, possibly distinct.
- Tensors – is an ordered collection ready for mass assigning like: (int, int) a = (2, 4). A special case of tensor type is the unit type (). It represents that a function doesn’t return any value, or has no arguments.
Currently, FunC has no support for defining custom types.
See Also
Declaring and using variables
Solidity is a statically typed language, which means that the type of each variable must be specified when it is declared.
uint test = 1; // Declaring an unsigned variable of integer type
bool isActive = true; // Logical variable
string name = "Alice"; // String variable