About Monkey 2

Monkey2 is a new programming language designed by Mark Sibly, creator of the ‘Blitz’ range of languages.

While staying true to the ‘basic’ style of the original blitz languages, Monkey2 offers some very powerful new features including:

Generic classes and methods.

Classes, interfaces, structs, methods and functions can have ‘type’ parameters:

Monkey
1
2
3
4
5
6
7
8
structRect<T>
Fieldx0:T,y0:T
Fieldx1:T,y1:T
End
FunctionMain()
Localr:=NewRect<Float>
End

‘First class’ functions.

Functions (and methods) can be stored in variables and passed to/from other functions.

Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FunctionTest1()
Print"Test1!"
End
FunctionTest2()
Print"Test2!"
End
FunctionTester(test:Void())
test()
End
FunctionMain()
Tester(Test1)
Tester(Test2)
End

Lambda functions.

Lambda functions allow you to create closures:

Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
FunctionTest(func:Void())
func()
End
FunctionMain()
ForLocali:=0Until10
Test(Lambda()
Printi
End)
Next
End

New ‘struct’ type that provides value semantics.

Structs are similar to classes in that they encapsulate member data, but differ in that they are passed around ‘by value’ instead of ‘by reference’.

This allows structs to be efficiently created on the stack without any garbage collection overhead.

Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
StructS
Fielddata:Int=10
End
FunctionTest(s:S)
s.data=100
End
FunctionMain()
Locals:=newS'Create a new S on the stack (very fast!)
Test(s)'Test gets a copy of 's'.
Prints.data'Print '10'
End

Fibers for easy asynchronous programming.

Fibers provide support for ‘cooperative’ multithreading:

Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FunctionServer(host:String,service:String)
Localserver:=Socket.Listen(host,service)
Repeat
Localclient:=server.Accept()
NewFiber(Lambda()
Localdata:=client.Receive(...)
End)
Forever
End

Operator overloading.

Operator overloading allows you to override the meaning of the built-in language operators, making for more expressive code:

Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
StructVec2
Fieldx:Float
Fieldy:Float
Method New(x:float,y:Float)
Self.x=x
Self.y=y
End
Operator+:Vec2(v:Vec2)
ReturnNewVec2(x+v.x,y+v.y)
End
Operator To:String()
Return"Vec2("+x+","+y+")"
End
End
FunctionMain()
Localv0:=NewVec2(10,20)
Localv1:=NewVec2(30,40)
Print v0+v1
End

Class extensions.

Class extensions allow you to add extra methods and functions to existing classes.

Fully garbage collected.

Monkey2 provides a ‘mostly’ incremental garbage collector that efficiently collects garbage as it runs without any of those annoying ‘sweep’ spikes found in typical garbage collectors.

Optional reflection features.

Monkey2 includes an optional reflection system that allows you to inspect and modify variables and values at runtime:

Monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#Import"<reflection>"
ClassC
Method Update(msg:String)
Print"C.Update : msg="+msg
End
End
FunctionMain()
Localc:=NewC
Localtype:=Typeof(c)
Print type
Localdecl:=type.GetDecl("Update")
decl.Invoke(c,"Hello World!")
End

Multi-target

Monkey2 works on a wide range of targets: Windows, Macos, Linux, Emscripten, Android and iOS.

23+