In the world of programming, each language has its unique features and charm. Today, we will delve into the FunC programming language, understanding its basic built-in types and the characteristics of its type system to help readers grasp the essence of this language more effectively.
Atomic Types: The Building Blocks of FunC
int
Type: Unlike common 32-bit or 64-bit integers, FunC’sint
type is a 257-bit signed integer. This design makes it safer for handling large number operations. It’s worth noting that by default, FunC enables overflow checking, and an exception will be thrown if integer overflow occurs.cell
Type: In the TON blockchain, all persistent data is stored in a tree of cells. Thecell
type represents a TVM cell, which can contain up to 1023 bits of arbitrary data and four references to other cells. In the stack-based TVM,cell
acts as memory.slice
Type: Acell
can be converted into aslice
, which allows us to read the data bits and references to other cells within the cell.builder
Type: Thebuilder
is used to construct new cells. During the construction process, we can store data bits and references to other cells in thebuilder
, which can then be transformed into a new cell.tuple
Type: Thetuple
type in FunC is an ordered collection that can contain up to 255 components of different types. This makes it very flexible when dealing with complex data structures.cont
Type: Thecont
type is a TVM continuation type used to control the flow of program execution. Although it is a relatively low-level object from the perspective of FunC, it is conceptually quite general-purpose.
Special Type Concepts: The Unique Aspects of FunC
- Representation of Boolean Values: FunC does not have a dedicated boolean type. Boolean values are represented by integers, where 0 stands for false and -1 (represented in binary as 257 ones) stands for true. Logical operations are implemented through bitwise operations.
null
Value: In some cases, the value of an atomic type may be missing. Thenull
value can be used to represent this. It’s important to note that usingnull
values is disabled by default and can lead to runtime exceptions.- Type Inference: FunC supports type inference, using
_
andvar
as type “holes” that are automatically filled with actual types during type checking. This simplifies type declarations in the programming process.
Composite Types: Building More Complex Data Structures
- Function Type: Represented as
A -> B
, it accepts a parameter of type A and returns a result of type B. This type is very important in FunC for defining various functionalities. - Tensor Type: Represented as
(A, B, ...)
, it is a collection of ordered values, each of which can be of type A, B, etc. Tensor types are useful when dealing with multiple return values. - Tuple Type: Represented as
[A, B, ...]
, it is a TVM tuple with a known length and component types at compile time. Unlike the empty tuple()
in thecell
type, the empty tuple[]
occupies one stack entry.
Polymorphism: Flexibility Across Different Types
FunC supports polymorphic functions with type variables, such as forall X -> (X, X) duplicate(X value)
. This kind of function can accept parameters of any type and return two copies of the same type.
User-defined Types and Type Width
Currently, FunC does not support user-defined types, but each type’s value occupies a certain number of stack entries, known as the type width. When defining polymorphic functions, the fixedness and predictability of the type width must be considered.
In summary, the type system of the FunC programming language is rich in features and flexibility. Mastering these characteristics will help us to use this language more effectively for programming.