NouvaLang v0.1.0
GitHub

Type Casting (as)

Learn how to explicitly convert values between primitive types using the as casting operator.


The as Operator

Because NouvaLang enforces strict type checking, you cannot implicitly pass or assign mismatched types. When conversion is needed, use the as operator.

1. Casting Numbers to String

Useful for formatted printing and string concatenation:

let score: int = 95;
let message: str = "Your score is: " + (score as str);
print(message); // "Your score is: 95"

2. Casting Floats to Integers (Truncation)

let pi: float = 3.99;
let integer_part: int = pi as int;
print(integer_part); // 3

3. Casting Integers to Floats

let count: int = 5;
let ratio: float = (count as float) / 2.0;
print(ratio); // 2.5

4. Casting Strings to Numbers

let num_str: str = "42";
let num: int = num_str as int;
print(num + 8); // 50