Skip to content

Variables and Types

DatAsiqn edited this page Nov 2, 2026 · 2 revisions

Defining Variables

In the previous example, we passed a string into a function as a parameter. If we wanted to use the same string again, we would have to write "Hello, World!" a second time. How cumbersome! Thankfully, Lantern allows you to declare variables, which hold values in a scope. Here's a simple variable declaration that stores the string "Hello, World!" in a variable called message:

val message = "Hello, World!";

Variable declarations start with the val keyword, followed by the name of the variable and an initializer. All variables need an initial value, and in this case the initial value of message is the string "Hello, World!". Variable declarations can also include an optional type annotation like val message: str = "Hello, World!". In the majority of cases though, type inference is able to automatically figure out this type for you.

Now, we can, for example, print that message to the standard output:

println(message);

In fact, we can print it multiple times:

println(message);
println(message);
println(message);

Which, when ran, outputs Hello, World! three times.

Variables can also be reassigned to using the = operator:

message = "Goodbye!";

Now, the message variable contains the string "Goodbye!" instead of "Hello, World!".

Types

Lantern is a strongly-typed language and will enforce type safety at compile-time. Types describe what a piece of data is, and every value has a type. Some native types include:

  • byte a byte, mainly used to represent string characters
  • int integers like -1, 5, 3, and 0
  • float floating-point numbers like 1.22, -0.5, and 4.0
  • bool a boolean value, either true or false
  • str a string of characters

There are additional types such as arrays, optionals, and functions, but those have special behavior and are covered later in this wiki.

Numeric types can perform also simple arithmetic operations:

  • + for addition
  • - for subtraction or negation
  • / for division
  • * for multiplication
  • % for modulus

and comparisons:

  • > for greater than
  • >= for greater than or equal to
  • < for less than
  • <= for less than or equal to
  • == for equal to

in infix notation (e.g. 1 + 2 * 5 or 5 > 3).

Type Safety

During compilation, Lantern will ensure type-safety across your entire codebase, and emit an error if there are any type mismatches. For example, say we define an integer variable:

val foo = 10;

We can reassign this variable to another integer, but trying to reassign it to a value that is not an integer will emit an error.

val foo = 10;
foo = "this is a string";
$ lantern main.la
error: expected int, but got str instead
  in main.la:5:9:

    foo = "this is a string";
          ^^^^^^^^^^^^^^^^^^ expected int, but got str instead

The error tells us that Lantern expected foo to be assigned to an integer, but we gave it a string instead.

Type checking also happens when calling functions, ensuring arguments are given the correct types. Try using println and passing an integer to it instead of a string and see what happens.

Clone this wiki locally