Type System & Null Safety
Autolang guarantees static type safety. The type system distinguishes between nullable and non-nullable types, virtually eliminating null reference errors unless intentionally bypassed.
Nullable Types and Non-Null Types
By default, regular types cannot hold null. To allow null, you declare a variable as a nullable type by appending a ? (e.g., String?).
var a: String = "abc"
// a = null // Compilation error!
var b: String? = "abc"
b = null // Valid!Optional Access / Safe Calls
If you try to call a method or access a property on a nullable type, the compiler will refuse unless you perform a safe call ?.. Safe calls return null if the receiver is null, instead of crashing the program.
class OptionalAccess(var child: OptionalAccess?) {
static val a = 7
val x = 0
val m = 0
func hello(value: Int) {
println("Hello world nha ${value}")
}
func get(): Int? {
return null
}
}
func testOptionalAccess() {
var a: OptionalAccess?
a?.hello(1) // Safe call, if 'a' is null, does nothing.
// Null coalescing: if 'a?.get()' returns null, return 100
println(a?.get() ?? 100)
a = OptionalAccess(a?.child)
a?.hello(2)
// Chaining safe calls
a?.child?.hello(3)
// Non-null assertion (!! or ! operator, depending on syntax variant):
// Forcefully dereferencing. If a is null, it throws an AVM error at runtime.
a!.child = OptionalAccess(null)
a?.child?.hello(4)
println(a?.a)
val m: OptionalAccess? = null
println(m) // Prints null
}
testOptionalAccess()