Constants in Nim

Our program will demonstrate constants of character, string, boolean, and numeric values. Here’s the full source code.

import math

const 
  s = "constant"
  n = 500000000
  d = 3.0e20 / n

echo s

# Const expressions perform arithmetic with arbitrary precision
echo d

# Numeric constants have no type until they are given one, such as by an explicit conversion
echo int64(d)

# A number can be given a type by using it in a context that requires one, such as a variable assignment or function call.
# For example, `math.sin` expects a `float64`.
echo sin(float64(n))

To run the program, save the code in constants.nim and use the nim compiler to execute it.

$ nim compile --run constants.nim
constant
6.0e+11
600000000000
-0.28470407323754404

In Nim, you declare constants using the const keyword. Numeric constants have no type until they are given one through context, such as variable assignments or function calls. Here, math.sin expects a float64.

Next example: For Loop.