;; KLOS object system tests

;;AUTOLOAD: Point

(defclass Point Object ((x :initform 0) (y :initform 0)))
(defun Point-y (obj) (getn obj 'y))
(defun Point-x (obj) (getn obj 'x))
(setq p (make-instance Point))

(if (not (boundp 'expt))
  (defun expt (mantiss exponent) mantiss); temporary hack
)

(defmethod dist ((p Point))
  "The distance from origin"
  (expt
    (+
      (expt (get p 'x) 2)
      (expt (get p 'y) 2))
    0.5
))

(defmethod dist ((p Int)) p)
(defun intpart (x) (coerce x Int))
(add-method Real 'dist intpart)

(put p 'x 1)
(put p 'y 1)

(add-method Point 'dist (lambda (p)
    (expt
      (+
	(expt (get p 'x) 2)
	(expt (get p 'y) 2))
      0.5
)))
(add-method Int 'dist (lambda (p) p))

(setq pl '(x 4 y 3 dist
    (lambda (p)(expt
	(+
	  (expt (get p 'x) 2)
	  (expt (get p 'y) 2))
	0.5
))))

(defclass Point3D Point ((z :initform 0)))

(defmethod make ((obj Point3D))
  (setq Last-Point-Sum
    (+ (get obj 'x)(get obj 'y)(get obj 'z))
))
  
(defmethod free ((obj Point3D)) (setq Last-Point-Sum ()))

(defmethod dist ((p Point3D))
  (expt
    (+ (expt (call-next-method) 2)
      (expt (get p 'z) 2))
    0.5
))

(setq p3 (make-instance Point3D 'x 1 'y 2 'z 3))
(make-instance Point3D  'y 2)

(defmethod dist ((p Point3D))
  (call-next-method (make-instance Point 'x 3 'y 4))
)
