Tutorial
The tutorial for Night is still in progress. View the GitHub page to see how you can contribute!
This tutorial offers a complete overview of the language Night. This is perfect for any beginners or anyone coding with Night for the first time. For a more in depth look at individual syntax and additional notes, check out the Reference page instead.
Basic:
Advanced:
Hello World!
For basic output to the console, you can use the print()
function. It takes a single parameter of any type, and displays it to the console. Singleline comments are also supported.
print(
# this is a comment
Variables
Night is gradually typed, and all variables are immutable by default. Night supports many common expressions, including string concatenation. It's best to assign types to variables when possible, and to leave then immutable.
let var = 2 + 3
var = "squ" + "id"
# 'float' and 'str' are also supported types
let can_swim: bool = true
let distance: int mut = 100
User Input
User input in Night takes the form of the function input()
. The function will return the user input in the form of a string. You can use the following casts to convert between types: int
, float
, or str
.
print('What is your name? ')
let name = input()
let age = int(input())
Conditionals
Conditionals in Night are similar to other languages. If there is only one statement nested in the conditional, curly bracket can be omitted.
let num = input()
if (num > 50)
{
print("too big!")
exit()
}
if (num < 0)
print("neg")
elif (num == 0)
print("zero")
else
print("pos");
Functions
Functions in Night support all five types, along with a null
(void) type. Regular type functions must have a return value, while null
type functions do not need a return value.
fn add(x, y)
{
return x + y
}
fn sub(x: int, y: int mut)
{
y += 1
return x - y
}
print(add(2, 3))
print(sub(9, 7))
Note that functions have to be defined before they are used.
Loops
There are two types of loops in Night, the while
and for
loop. The while
loop will only run if its condition evaluates to true
, and the for
loop will only run a prespecified amount of times.
let arr = [ 5, 6, 7 ]
for (x : arr)
print(str(x) + " ")
Arrays
Arrays in Night can contain elements of multiple types. There's a variety of built in methods to assist array functions, all of which are documented in the reference page.
let arr = [ 5, 6, 6 ]
arr.pop()
arr.push(7)
print(arr[0])
print(arr)
Type Checking
What differs Night from other dynamically typed languages is its type safety.
Each variable stores an array of types. Using it in expressions can add types to the list. For example if
a variable is used with the multiplication operator, then the type integer and float is added
to the variable. This allows for accurate type safety while also maintaining a dynamically
typed system.
let var = 's' # 'var' contains type: 'str'
var = true # 'var' contains type: 'str', 'bool'
def mult(x) {
return x * 5
}
print(mult(var)) # 'var' contains type; 'str', 'bool', 'int', 'float'