Class instantiation

Started by electrifiedspam, November 28, 2010, 01:37:17 AM

Previous topic - Next topic

electrifiedspam

I am a little confused.



I can have a class

--------------------------------

(context aClass)

(new Class)



(context 'MAIN)

--------------------------------

now---

(set 'aClass:x 5)

5

(set 'foo aClass)

(aClass)



AND----

foo:x

5



So my question is, is it possible to instantiate a new class, or am I simply creating a new context.



(new aClass 'newClass)



creates a new context called newClass that inherits from the old class aClass.



Is this the way we achieve polymorphism, or have I really missed something? I suppose this does the trick but comming from C# it just feels a little loose to me.



Thank you for your help

Chris

electrifiedspam

#1
I have read Lutz's paper again.



I think that my hang up is that I need to think in functional terms. So storing anything in a class is a bad idea unless it is a true constant like 3.14.



So a class is really just all of the rules and methods for handling the data.



Anyway I just wanted to say that this is a really great community and a very informative forum.



Thank you all



Chris

cormullion

#2
I think there are many ways to approach this type of programming.



The FOOP style is officially sanctioned and has support in the base system. And michael's videos explain it well.



Greg's ObjNL is "a new and exciting way of doing real object oriented programming in newLISP where instances are passed by reference and can easily hold references to other objects while maintaining their own mutable state", and is probably aimed at programmers like yourself. It is very compact, too.



Fanda (once a forum regular) wrote a minimal OOP system, but I can't track down the code for that anymore.

electrifiedspam

#3
I am trying to understand foop.



Given the class:

-----------------------------------------------------------------------------

;;obstruction class

;;This is an object that has a point and can return that point.

(context 'OBS)

(new Class)

(define (OBS:OBS POINTS) (cons (context) POINTS)) ;;new constructor

(define (body) (self 1));; Returns the body of the obstruction in an point (x y)

(define (addPt x y) (append (list (list x y)) (list (self 1))))



(context 'MAIN)

-----------------------------------------------------------------------------

;;I can instantiate a new object

(set 'test '(OBS (1 2)))

(OBS (1 2))



;;I can send messages to that object

(:addPt test (3 4))

((3 4) (1 2))



;;BUT

(:body test)

;; returns

(1 2)



Is there an easier way to update the points?

(set 'POINTS '(1 2))

(set 'test '(OBS POINTS))

(set 'POINTS (:addPt test 3 4))

(set 'test '(OBS POINTS))



Is the only way that I can think to do it, and it does not seem like good foop to me. Is there a simpler way to do this?

electrifiedspam

#4
I changed:

(define (addPt x y) (append (list (list x y)) (list (self 1))))



to



(define (addPt x y) (setf (self 1) (append (list (list x y)) (list (self 1)))))



That seemed to do the trick. Now I can have player 1, 2, 3, 4, 5, 6, 7..... and the game state for that player follows the object.



Any input is welcome



Thank you.

Chris

Lutz

#5
This is how you could do it:
; define methods in an hygienic way
(context 'OBS)
(define (OBS:OBS POINTS) (cons (context) POINTS))
(define (body) (self 1))
(define (addXY x y) (push (list x y) (self) 1))
(context MAIN)

; instantiate new OBS
(set 'test '(OBS (1 2)))
; (set 'test (OBS '(1 2))) ; alternatively using the constructor function

; add new coordinates in front
(:addXY test 3 4)

; object test is now modified
test ;-> (OBS (3 4) (1 2))

Note, that doing:
(new Class 'OBS)
is just a shortcut for doing:
(context 'OBS)
(define (OBS:OBS) (cons (context) (args)))
(context MAIN)