Io is a small, prototype-based programming language inspired by Smalltalk, Lisp, and Lua. It is designed to be simple, reflective, and highly extensible, making it a popular choice for domain-specific languages, scripting, and rapid prototyping.
Variables in Io are declared using the :=
operator. Io supports dynamic typing, so you don’t need to specify the type of the variable.
name := "John"
age := 30
pi := 3.14
Functions in Io are objects that can be assigned to variables, passed as arguments to other functions, and returned as values. Io supports dynamic message passing, which means that functions can be called on any object, not just classes.
greet := method(name,
"Hello, #{name}!" print
)
greet("John")
add := method(a, b,
a + b
)
add(2, 3) println
Io supports while
and foreach
loops, as well as the loop
control structure, which can be used to create infinite loops.
numbers := list(1, 2, 3, 4, 5)
foreach(number, numbers,
number println
)
i := 0
while(i < 5,
i println
i = i + 1
)
loop(
"This is an infinite loop" println
)
Io supports if
, else if
, and else
statements, as well as the ternary operator.
age := 30
if(age < 18, "You are too young to vote" println,
if(age < 21, "You can vote, but not drink" println,
"You can vote and drink" println
)
)
result := age >= 18 ? "You are an adult" : "You are not an adult"
result println
Io provides several ways to read and write files. You can use the File
object to create, read, write, and delete files.
file := File with("example.txt")
// Write to file
file write("Hello, world!")
// Read from file
content := file readAll
content println
// Delete file
file delete