Converting monkey1 code to monkey2
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:
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
Note: This section highly subject to change.
In monkey2, there is no need to import modules as all modules are now automagically imported for you.
However, it is still necessary to know what ‘namespaces’ the various declarations (ie: function, classes etc) you want to use are located in.
The easiest way to deal with this for now is to add this to the top of your source files:
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:
Function call parameters must be enclosed in parenthesis
In monkey1, you could (usually…) omit the paranthesis around function parameters, eg:
In monkey2, this is not allowed - all function parameters must be enclosed in parenthesis, eg:
Array initializers must use New
Monkey1 allowed a ‘quick’ form of array initializer:
In monkey2, you need to use the longer (but equivalent) form:
Virtual and overriding methods must be explicitly marked
Note: This section highly subject to change.
In monkey1, all non-final methods are automatically virtual and can (sometimes inadvertantly) dynamically override existing methods. In monkey2, virtual and overriding methods must be marked as such, eg:
Property syntax changes
Code that declares properties will need to use the new syntax. Instead of this…
…you will to need to use…
End-of-line handling
Note: Subject to change…
Monkey2 uses similar end-of-line handling to monkey1, but is stricter.
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:
Multiple statements can appear on a single line, as long as they are separated by the statement separator token ‘;’, eg:
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 (yet?).
Instead of this…
…use this…