Hi,
A quick list of things to consider when converting monkey1 code to monkey2…
Strict mode not an option!
All functions that return something must return something! In monkey1, you could omit return statements even in functions that returned non-void. Not so in monkey2 (although this isn’t actually implemented in the compiler yet..).
Variables no longer default to int – all variable declarations must either specify a type, or be initialized with a value, eg:
1
2
3
4
5
6
7
8
|
Local blah 'Wont work in monkey2!
Local blah:Int 'Ok in monkey1 and monkey2.
Local blah:=10 'Ok in monkey1 and monkey2 - blah is int.
Local blah:Int=10 'Ok in monkey1 and monkey2
|
Method and function declarations can still omit a return type, however the default return type in monkey2 is void (as opposed to int in monkey1).
The monkey1 shortcut type specifiers (eg: % for int and # for float) are not supported in monkey2 – you must use ‘:Int’, ‘:Float’ etc.
Using namespaces and importing files
Monkey2 adds a ‘namespace’ system to monkey1.
This basically means all declarations belong to some namespace. There are no ‘global’ declarations.
The easiest way to deal with this is to add this to the top of your source files:
1
2
3
4
5
|
#Import "<std>"
#Import "<mojo>"
Using std..
Using mojo..
|
This will allow you to access ALL decalarations in the std and mojo namespaces.
To import monkey2 source files into a build, use the preprocessor #Import directive, eg:
1
2
|
#Import "file2"
|
If you don’t specify an extension, #Import assumes ‘.monkey2’ so this actually imports “file2.monkey2”
Function call parameters must be enclosed in parenthesis
In monkey1, you could (usually…) omit the paranthesis around function parameters, eg:
1
2
|
DrawImage image,x,y 'will not work in monkey2!
|
In monkey2, this is not allowed – all function parameters must be enclosed in parenthesis, eg:
1
|
DrawImage( image,x,y ) 'works in monkey1 and monkey2
|
Array creation syntax changes
Monkey1 allowed a ‘quick’ form of array constructor:
1
2
|
Local tmp:Int[10] 'wont work in monkey2!
|
In monkey2, you need to use the longer (but equivalent) form:
1
2
|
Local tmp:=New Int[10] 'works in monkey1 and monkey2!
|
Also, to create an array with existing elements you must now use:
1
2
|
Local tmp:=New Int[]( 1,2,3,4 ) 'works in monkey2 only - creates an array of length 4.
|
This replaces the old [value0,value1,value2…] syntax.
Virtual and overriding methods must be explicitly marked
In monkey1, all non-final methods are automatically virtual and can (sometimes inadvertently) dynamically override existing methods. In monkey2, virtual and overriding methods must be marked as such, eg:
1
2
3
4
5
6
7
8
9
10
|
Class Base
Method Update() Virtual
End
End
Class Derived Extends Base
Method Update() Override
End
End
|
Property syntax changes
Code that declares properties will need to use the new syntax. Instead of this…
1
2
3
4
5
|
Method MyProp:String() Property 'monkey1 'getter'
End
Method MyProp( arg:String ) Property 'monkey1 'setter'
End
|
…you will to need to use…
1
2
3
|
Property MyProp:String() 'monkey2 only!
Setter( myProp:String )
End
|
End-of-line handling
Monkey2 is currently VERY strict about end-of-lines.
All ‘block’ declarations (Class, Method, Property, Function) and block statements (If, While, Repeat, Select) must be of the following form:
_header_
…_content_…
_footer_
_header_ and _footer_ must start on a new line, and be the only thing on that line. Declarations can no longer be ‘mashed up’ onto a single line. For example:
1
2
3
4
5
|
Class MyClass Extends BaseClass 'class declaration header
Method Update() 'method declaration header
End 'method declaration footer
End 'class declaration footer
|
Multiple statements can appear on a single line, as long as they are separated by the statement separator token ‘;’, eg:
1
2
3
4
|
For Local i:=0 Until 100
Print( i );Print( i*2 );Print( i*3)
Next
|
Finally, monkey2 ignores any end-of-line tokens that appear after any of the following: ‘(‘, ‘[‘ and ‘,’. This allows you to split function parameter lists and auto array elements over mulitple lines.
No fancy slices.
Instead of this…
1
2
3
4
|
Local x:=t[x..] 'monkey1 only!
Local y:=t[..y]
Local z:=t[x..y]
|
…use this…
1
2
3
4
|
Local x:=t.Slice( x ) 'monkey2 only!
Local y:=t.Slice( 0,y )
Local z:=t.Slice( x,y )
|
Bye,
Mark
I thought I was looking forward to shadows but I think depth of field effects are going to be my favourite feature of this new stack.
I want it could replace some other languages in recognition someday because it has the finest potential I’ve seen in any new languages they’ve been released over the years. Best Essay Writing Service