Understanding FunC Programming Language: Exploring Its Built-in Types and Type System Features

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

  1. int Type: Unlike common 32-bit or 64-bit integers, FunC’s int 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.
  2. cell Type: In the TON blockchain, all persistent data is stored in a tree of cells. The cell 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.
  3. slice Type: A cell can be converted into a slice, which allows us to read the data bits and references to other cells within the cell.
  4. builder Type: The builder is used to construct new cells. During the construction process, we can store data bits and references to other cells in the builder, which can then be transformed into a new cell.
  5. tuple Type: The tuple 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.
  6. cont Type: The cont 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

  1. 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.
  2. null Value: In some cases, the value of an atomic type may be missing. The null value can be used to represent this. It’s important to note that using null values is disabled by default and can lead to runtime exceptions.
  3. Type Inference: FunC supports type inference, using _ and var 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

  1. 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.
  2. 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.
  3. 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 the cell 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.