Skip to content

Types

Manul is statically type, meaning each variable and expression must have a knwon type at compile type. The type can be either explicit or inferred.

Primitive types are those provided by the language, they include: byte, short, int, long, float, double, bool, char and string.

Numeric Types

There’re four integer types with varying bit lengths:

byte: 8 bit. short: 16 bit. int: 32 bit. long: 64 bit.

float and double are floating-oint numbers. float is 32-bit. double is 64-bit.

Together, integers and floating-point numbers are called numeric types.

The following operators are supported for numeric types: +, -, *, \, %.

// addition
val sum = 1 + 1
// subtraction
val difference = 4 - 1
// multiplication
val product = 1.5 * 2
// division
val quotient = 5.2 / 2
// remainder
val remainder = 5 % 2

Boolean Type

The bool type represents logical values: true and false. It supports the logical AND (&&) and the logical OR (||) operators.

val t = true
val f = false
val x = t && f
val y = t || f

Character Type

The char type is used to represent characters.

val a = 'a'
val b = '好'

Array types are used to represent a list of values. The syntax consists of the element type followed by [].

To create an array, use the () constructor syntax.

Use the append method to add an element to the end of the array. Use array[index] to access an element.

// Create a new array
val a = int[]()
// Add an element to the array
a.append(1)
// Access the first element
print(a[0])
// Modify the first element
a[0] = 2

Classes are user-defined types. The following example declares a User class:

class User(
var name: string,
var email: string
)

Class can be used as just like any other type:

var user: User

We will discuss class in more details later.

Union types allow a value to be one of several different types. For example, int | string describes a value that can be either an integer or a string.

A common use case for union types is handling nullability. A type that can either be a string or null is written as string | null. For convenience, this can be written using the shorthand syntax: string?.

Examples:

// Integer or string
int | string
// Three alternative types
int | long | float
// Nullable type
string | null
// Nullable type in shorthand syntax
string?