Variables
Variables are containers for storing data values. Use val (immutable) or var (mutable) and assign a value with =.
Syntax
The compiler will infer the type from the assigned value, or you can declare it explicitly. var is mutable; val is immutable.
val variableName = value
var variableName = value
// Explicit type
val variableName: type = value
// Wrong: compiler can't infer the type without a value
val variableName
// Wrong: val is immutable
val variableName = 1
variableName = 2Default Types
| Type | Description |
|---|---|
| Int | Whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Character literals (e.g. a) are also stored as Int. |
| Float | Fractional numbers. Sufficient for 15–16 decimal digits. |
| Bool | Stores true or false. |
| String | Stores a sequence of characters. |
| Any | Stores any object (testing). |
Character Literals
In Autolang, there is no separate Char type. Single-quote character literals evaluate directly to their Int (ASCII) values.
val codeA = 'a' // codeA is Int with value 97
val codeB = 'b' // codeB is Int with value 98
println(codeA + 1 == codeB) // Output: trueNullable Types
Append ? to a type to allow null. Use ! to assert non-null.
val variableName: type? // Nullable, default null
var variableName: type? // Nullable, mutable
val variableName? = value // Inferred nullable
val variableName! = value // Inferred non-nullableAssignment & Copying
Int values are automatically copied on assignment (passed by value). Other object types are references by default. To share a mutable reference to an Int, wrap it in a class.
var a = 5
var b = a // b gets a copy of a
b += 1
println(a) // 5 (unchanged)
// Use a wrapper class to share a mutable reference
class Box<T>(var value: T)
val boxA = Box<Int>(10)
val boxB = boxA // both point to the same Box
boxB.value += 5
println(boxA.value) // 15String Concatenation with Objects
When you concatenate an object with a String using +, Autolang automatically calls toString() on that object.
class User(val username: String) {
fun toString(): String = "User: " + username
}
val u = User("bob")
println(u + " is logged in") // Output: User: bob is logged in
println("Welcome, " + u) // Output: Welcome, User: bobExample
val name = "John" // String
val age = 18 // Int
val char: Int = 'A' // Character literal as Int (65)
val money: Float = 4.5 // Float
val email! = """autolang@gmail.com
"""
val child: Int? // Null value
println("Hello everyone, my name is " + name + " and I'm " + age)
println("Hello everyone, my name is ${name} and I'm ${age}")
println("Now I have $money dollar and my email is $email")
if (child == null) {
println("I don't have any childs")
} else {
println("I have some childs")
}