How to deal with lists of objects?
Posted: Sat Apr 26, 2014 7:14 am
Hello!
I am trying to set up a little simulation project, but I am running into some problems with FOOPS and pass by value...
I have a Plane class, which considers the engine RPM and the altitude of the airplane:
Now, I might have methods that change the internal state of a particular instance of the Plane object. Here is one that firewalls the engine (goes to full throttle for a climb):
This seems to work well enough for a single named instance of Plane:
Which produces:
However, if I try to create a list of many airplanes:
This is what happens:
As you can see, nothing is changed. Upon further investigation (printing "p" after the :firewall call) I see that the intermediate object p is changed, but not stored to the list "planes". I assume that this is happening because as dolist iterates through planes, each Plane object is copied to p.
What is the best way to handle updating an object in a list of objects like this?
Also, for what it's worth, I have considered modifying :firewall to return self so I can do this:
But I hope you can tell me there's a better way to handle it!
Thanks!
I am trying to set up a little simulation project, but I am running into some problems with FOOPS and pass by value...
I have a Plane class, which considers the engine RPM and the altitude of the airplane:
Code: Select all
(new Class 'Plane)
(define (Plane:Plane rpm alt)
(list 'Plane rpm alt))
Code: Select all
(define (Plane:firewall)
(setf (self 1) 2700))
Code: Select all
(setq aplane (Plane 1900 2000))
(println "Original: " aplane)
(:firewall aplane)
(println "Modified: " aplane)
Code: Select all
Original: (Plane 1900 2000)
Modified: (Plane 2700 2000)
Code: Select all
(setq
planes (list
(Plane 2700 0)
(Plane 2100 1250)
(Plane 1500 900)))
(println "Original: " planes)
(dolist (p planes) (:firewall p))
(println "Modified: " planes)
Code: Select all
Original: ((Plane 2700 0) (Plane 2100 1250) (Plane 1500 900))
Modified: ((Plane 2700 0) (Plane 2100 1250) (Plane 1500 900))
What is the best way to handle updating an object in a list of objects like this?
Also, for what it's worth, I have considered modifying :firewall to return self so I can do this:
Code: Select all
(dotimes (i (length planes))
(setf (planes i) (:firewall (planes i))))
Thanks!