Constants

Constants, unlike variables, are always immutable. You cannot apply the mut keyword to them to make constant mutable.

Unlike variables, constants can only be declared outside functions, that is, in the scope of a module. Constants are declared using the const keyword:

const PI = 3.14

As with variables, constants must always have a value. By convention, constant names are written in capital letters separated by underscores.

Constants are useful when you need to define data that will be used throughout the program, for example, config paths, program version, colors, etc.

As with variables, the type of constant is inferred from its value.

Create a file constants.sp and add the following code to it:

file=constants.sp import os const ( PROGRAM_NAME = 'Spawn Test Program' VERSION = 'v0.1.0' ) fn main() { if '--help' in os.ARGS { println('Usage: ${PROGRAM_NAME}') println('Version: ${VERSION}') return } println('Hello, world!') }

Now, let's compile it and run:

spawnc --run constants.sp

If we don't pass any arguments at startup, the code in the if block will not execute (we'll talk about if a bit later) and the program will print Hello, world!:

./out
Hello, world!

But if we pass the --help argument, the program will print help:

./out --help
Usage: Spawn Test Program
Version: v0.1.0

As you may have noticed, we also used the ARGS constant from the os module, we'll talk about imports and modules later, but for now just remember that this constant contains a list of arguments passed to the program at startup.

You may also have noticed that multiple constants can be declared in one block, this allows constants to be grouped by meaning, removes visual noise due to the repeated const keyword, and makes the code more comfortable to read since all constant values are aligned vertically:

const ( PROGRAM_NAME = 'Spawn Test Program' VERSION = 'v0.1.0' ) // vs const PROGRAM_NAME = 'Spawn Test Program' const VERSION = 'v0.1.0'

Unlike other languages, the value of a constant does not need to be computable at program compile time; the following definitions of constants are allowed:

const ( RED = Color{ r: 255, g: 0, b: 0 } LIGHT_RED = RED.lighten(0.5) )

The value of the second constant will be evaluated when the program starts. Such constants are useful if you want to initialize the data with some calculations, but at the same time leave them immutable.

Constants can be used as initializers for other constants as shown in the example above. However, be careful that if two constants refer to each other, this will result in a compilation error since the compiler will not be able to determine the order in which they are initialized.

Constants remain valid throughout the program's execution, which makes constants a convenient way to define data that can be used in different program modules. Constants are also useful for naming magic numbers, for example, the time module defines the constant SECOND which is initialized to 1_000_000_000.

Using the SECOND constant instead of the 1_000_000_000 number makes the code more readable and understandable.

:= or =

Although variables are defined using the := operator, constants are defined using the = operator, the compiler will prompt you if you suddenly try to use := to define a constant:

error(E0020): expected `=` but got `:=`
 --> constants.sp:4:18:20
   |
 4 |     PROGRAM_NAME := 'Spawn Test Program'
   |                  ^^
   |                  |
   |                  =

Now that we know about variables and constants, it's time to look at what data types Spawn supports.

On this page