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!".

Clone this wiki locally