|
@@ -5,17 +5,17 @@
|
|
|
|
|
|
To declare a class:
|
|
|
|
|
|
-`Class` _Identifier_ [ `Extends` _SuperClass_ ] [ `Implements` _Interfaces_ ] [ _Modifiers_ ]
|
|
|
-```
|
|
|
- ...Class members...
|
|
|
-```
|
|
|
+<div class=syntax>
|
|
|
+`Class` _Identifier_ [ `<` _GenericTypeIdents_ `>` ] [ `Extends` _SuperClass_ ] [ `Implements` _Interfaces_ ] [ _Modifier_ ]
|
|
|
+ ...Class Members...
|
|
|
`End`
|
|
|
+</div>
|
|
|
|
|
|
_SuperClass_ defaults to `Object` if omitted.
|
|
|
|
|
|
_Interfaces_ is a comma separated list of interface types.
|
|
|
|
|
|
-_Modifiers_ can be one of:
|
|
|
+_Modifier_ can be one of:
|
|
|
|
|
|
* `Abstract` - class cannot be instantiated with 'New', it must be extended.
|
|
|
* `Final` - class cannot be extended.
|
|
@@ -26,11 +26,11 @@ Classes can contain consts, globals, fields, methods, functions and other user d
|
|
|
|
|
|
To declare a struct:
|
|
|
|
|
|
-`Struct` _Identifier_
|
|
|
-```
|
|
|
+<div class=syntax>
|
|
|
+`Struct` _Identifier_ [ `<` _GenericTypeIdents_ `>` ]
|
|
|
...Struct members...
|
|
|
-```
|
|
|
`End`
|
|
|
+</div>
|
|
|
|
|
|
A struct can contain consts, globals, fields, methods, functions and other user defined types.
|
|
|
|
|
@@ -48,26 +48,32 @@ Structs are similar to classes, but differ in several important ways:
|
|
|
|
|
|
To declare an interface:
|
|
|
|
|
|
-`Interface` _Identifier_ [ `Extends` _Interfaces_ ]
|
|
|
-```
|
|
|
+<div class=syntax>
|
|
|
+`Interface` _Identifier_ [ `<` _GenericTypeIdents_ `>` ] [ `Extends` _Interfaces_ ]
|
|
|
...Interface members...
|
|
|
-```
|
|
|
`End`
|
|
|
+</div>
|
|
|
|
|
|
_Interfaces_ is a comma separated list of interface types.
|
|
|
|
|
|
An interface can contain consts, globals, fields, methods, functions and other user defined types.
|
|
|
|
|
|
-Interface methods are always 'abstract' and cannot declared any code.
|
|
|
+Interface methods are always 'abstract' and cannot declare any code.
|
|
|
|
|
|
|
|
|
#### Fields
|
|
|
|
|
|
Fields are variables that live inside the memory allocated for an instance of a class or struct. To declare a field variable:
|
|
|
|
|
|
+<div class=syntax>
|
|
|
`Field` _identifier_ `:` _Type_ [ `=` _Expression_ ]
|
|
|
+</div>
|
|
|
+
|
|
|
...or...
|
|
|
+
|
|
|
+<div class=syntax>
|
|
|
`Field` _identifier_ `:=` _Expression_
|
|
|
+</div>
|
|
|
|
|
|
For struct fields, _Expression_ must not contain any code that has side effects.
|
|
|
|
|
@@ -76,11 +82,11 @@ For struct fields, _Expression_ must not contain any code that has side effects.
|
|
|
|
|
|
To declare a method:
|
|
|
|
|
|
-`Method` _Identifier_ [ `:` _ReturnType_ ] `(` _Arguments_ `)` [ _Modifiers_ ]
|
|
|
-```
|
|
|
+<div class=syntax>
|
|
|
+`Method` _Identifier_ [ `<` _GenericTypeIdents_ `>` ] [ `:` _ReturnType_ ] `(` _Arguments_ `)` [ _Modifiers_ ]
|
|
|
...Statements...
|
|
|
-```
|
|
|
`End`
|
|
|
+</div>
|
|
|
|
|
|
_ReturnType_ defaults to `Void` if omitted.
|
|
|
|
|
@@ -101,20 +107,65 @@ Methods are 'Final' by default.
|
|
|
|
|
|
To declare a read/write property:
|
|
|
|
|
|
+<div class=syntax>
|
|
|
`Property` _Identifier_ `:` _Type_ `()`
|
|
|
-...getter code...
|
|
|
-`Setter` `(` _Identifier `:` _Type_ `)`
|
|
|
-...setter code...
|
|
|
+ ...getter code...
|
|
|
+`Setter` `(` _Identifier_ `:` _Type_ `)`
|
|
|
+ ...setter code...
|
|
|
`End`
|
|
|
+</div>
|
|
|
|
|
|
To declare a read only property:
|
|
|
|
|
|
+<div class=syntax>
|
|
|
`Property` _Identifier_ `:` _Type_ `()`
|
|
|
-...getter code...
|
|
|
+ ...getter code...
|
|
|
`End`
|
|
|
+</div>
|
|
|
|
|
|
To declare a write only property:
|
|
|
|
|
|
-`property` `(` _Identifier `:` _Type_ `)`
|
|
|
-...setter code...
|
|
|
+<div class=syntax>
|
|
|
+`Property` `(` _Identifier_ `:` _Type_ `)`
|
|
|
+ ...setter code...
|
|
|
+`End`
|
|
|
+</div>
|
|
|
+
|
|
|
+#### Conversion Operators
|
|
|
+
|
|
|
+You can also add 'conversion operators' to classes and structs. These allow you to convert from a custom class or struct type to an
|
|
|
+unrelated type, such as another class or struct type, or a primitive type such as String.
|
|
|
+
|
|
|
+The syntax for declaring a conversion operator is:
|
|
|
+
|
|
|
+<div class=syntax>
|
|
|
+`Operator To` [ `<` GenericTypeIdents `>` ] `:` _Type_ `()`
|
|
|
+ ...Statements...
|
|
|
`End`
|
|
|
+</div>
|
|
|
+
|
|
|
+Conversion operators cannot be used to convert a class type to a base class type, or from any type to bool.
|
|
|
+
|
|
|
+For example, we can add a string conversion operator to the above Vec2 class like this:
|
|
|
+
|
|
|
+```
|
|
|
+Struct Vec2
|
|
|
+
|
|
|
+ ...as above...
|
|
|
+
|
|
|
+ Operator To:String()
|
|
|
+ Return "Vec2("+x+","+y+")"
|
|
|
+ End
|
|
|
+End
|
|
|
+```
|
|
|
+
|
|
|
+This will allow Vec2 values to be implictly converted to strings where possible, for example:
|
|
|
+
|
|
|
+```
|
|
|
+Local v:=New Vec2
|
|
|
+
|
|
|
+Print v
|
|
|
+```
|
|
|
+
|
|
|
+We no longer need to use '.ToString()' when printing the string. Since Print() takes a string argument, and Vec2 has
|
|
|
+a conversion operator that returns a string, the conversion operator is automatically called for you.
|