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
Section titled “Primitive Types”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: +, -, *, \, %.
// additionval sum = 1 + 1
// subtractionval difference = 4 - 1
// multiplicationval product = 1.5 * 2
// divisionval quotient = 5.2 / 2
// remainderval remainder = 5 % 2Boolean Type
The bool type represents logical values: true and false. It supports the logical AND (&&) and the logical OR (||) operators.
val t = trueval f = false
val x = t && fval y = t || fCharacter Type
The char type is used to represent characters.
val a = 'a'val b = '好'Array Types
Section titled “Array Types”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 arrayval a = int[]()
// Add an element to the arraya.append(1)
// Access the first elementprint(a[0])
// Modify the first elementa[0] = 2Classes
Section titled “Classes”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: UserWe will discuss class in more details later.
Union Types
Section titled “Union Types”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 stringint | string
// Three alternative typesint | long | float
// Nullable typestring | null
// Nullable type in shorthand syntaxstring?