(2) Methods are defined in a context with the class name.
Code: Select all
(define (rectangle:area p)
(mul (p 3) (p 4)))
(define (circle:area c)
(mul (pow (c 3) 2) (acos 0) 2))
(set 'myrect '(rectangle 5 5 10 20)) ; x y width height
(set 'mycircle '(circle 1 2 10)) ; x y radius
(3) we define the : (colon) as a function which extracts the class name from the object, constructs the class:method symbol and applies it to the object:
Code: Select all
(define-macro (: _func _obj)
(let (_data (eval _obj))
(apply (sym _func (_data 0)) (list _data))))
Code: Select all
(:area myrect) => 200
(:area mycircle) => 314.1592654
The above macro will be a built-in primitive in the next development version for maximum speed an minimum overhead. The : function solves the polymorphism problem. Objects defined as in rule (1) can be anonymous and can be nested.
Lutz
ps:
- defining the : colon doesn't disturb any existing functionality related to contexts
- subclassing is done using (new 'Class 'Subclass) and adding or overwriting methods in 'Subclass' doing: (define (Subclass:some-method) ...)
- multiple inheritance is possible using repeated 'new' (new 'Aclass 'Subclass), (new 'Bclass 'Subclass). This can add methods from 'Aclass and 'Bclass to an already existing 'Subclass.
- 'def-new' can be used to do mix-ins of single methods.
- a 'def-type' macro could be used to define <class-name>:<class-name> as a default functor to construct objects (<class-name> p1 p2 ...), but isn't really essential although convenient.