Marvin3 and Object Orientation
Marvin3 supports object-oriented programming through the use of classes. Inheritance is not supported at the moment but it will surely be added in the later stages of the development.
To declare a class you use @ symbol.
[constructing code] [me]
:method1-name [method2 code] ;
:method2-name [method2 code] ;
For example, consider the following 2D point class
!y !x me
:x %x ;
:y %y ;
:distance lhs .x %x - sqr lhs .y %y - sqr sqrt ;
To instantiate a Point
5 10 Point !point-a # create a point at(5,10)
42 4 Point !point-b # create a point at (42,4)
%point-a %point-b .distance println
Marvin3 classes have the following properties:
- All data is private, all methods are public
- The constructor is not a special method as it is
in many languages(e.g., Python's __init__), it is the body of the class.
- In class, use me keyword to access to the instance (just like C++'s this or Object Pascal's self)
- If you want your class to leave the instance on the stack, you must push the instance on the stack by calling me in the body.
- To create a new member variable or modify an existing one, simply popto (e.g., !y). To read one, pushfrom (e.g., %y)
- You can read the values of global variables, if they are not shadowed by a member variable
- You can access methods inside a class by, me .method-name
- You can instantiate a class by simply calling it
- A method of an object can be called by .method-name if the object is on the stack