Quick Tour of the Jogo Programming Language - mfichman/jogo GitHub Wiki
This page is a brief summary of the Jogo syntax, for folks who want to get a feel for the language. Check out the full tutorial for more depth.
Expressions
Io::println('hello world')
# Local type inference
x = 1 + 2
# Explicit type declaration
y Int = 9
Control structures
# Conditionals
if x == 9 {
Io::println('x is 9')
} else {
Io::println('x is not 9')
}
# Conditional match
match variable {
with 8:
Io::println('matched 8')
with 7:
Io::println('matched 7')
default:
Io::println('default')
}
# Type match
match any {
with String:
Io::println('matched String')
with Int:
Io::println('matched Int')
}
# While loop
while condition {
Io::println('loop')
}
# For-each loop
for leaf in tree {
Io::println(leaf)
}
Collections
# Array
array = ['one', 'two', 'three', 'four']
array = Array[String](10)
array[0] = 'five'
# Hash
hash = {one: 'two', three: 'four'}
hash = Hash[String, String]()
hash['one'] = 'five'
Functions
# Free function
add(first Int, second Int) Int {
ret first + second
}
# Closure
message = 'hello world'
greet = func() { Io::println(message) }
greet()
Data Structures
# Objects (reference types)
Truck < Object {
passengers = 0
@init(passengers Int) {
self.passengers = passengers
}
drive() {
Io::println("rollin' out")
}
}
truck = Truck(2)
truck.drive()
truck.passengers = 1
# Objects (value types)
Vector3 < Value {
x Int
y Int
z Int
}
vec1 = Vector3()
vec1.x = 10
vec2 = vec1
# Copy by value
# Enums
Fruit = APPLE | BANANA | PEAR | ORANGE
# Tagged unions
Choice = String | Int | Truck
Modules
import Io, File
println('opening a file')
file = open('file.txt', 'r')
Interfaces
# Structural typing
Vehicle < Interface {
drive()
}
drive(vehicle Vehicle) {
vechicle.drive()
}
drive(new Truck())
Generics
Box[:a] < Object {
object :a
}
box = Box[String]()
box.object = 'abc'
Coroutines
i = 100
task = Coroutine() func() {
while i > 0 {
Io::println(i.str)
yield
}
}
task()
task()