Skip to content
Extraits de code Groupes Projets
Valider 8ea7e54b rédigé par Nicolas Verbois's avatar Nicolas Verbois
Parcourir les fichiers

Update step_2+3a.rkt : handling inappropriate messages for multiple message in...

Update step_2+3a.rkt : handling inappropriate messages for multiple message in send (send p 'bar 2) + sety func
parent 4b4ee1a7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -7,14 +7,16 @@
; It will allow us to improve the syntax of the code, by allowing to
; send messages to objects in a clearer manner.
; Defined below, the send operater first looks if the receiver object is indeed
; an appropriate receiver object, else it returns a specific error.
; Defined further below, the send operater first looks if the receiver object is
; indeed an appropriate receiver object, else it returns a specific error.
; Then, it will try to apply the message.s to the object.
; A point object, constituted of two coordinates and returning a self function.
(define (point x y)
(define (setx value)
(set! x value))
(define (sety value) ;added a sety function for consistency
(set! y value))
(define (add p)
(point (+ x (p 'getx)) (+ y (p 'gety))))
(define (self m)
......@@ -22,7 +24,8 @@
((eq? m 'gety) y)
((eq? m 'type) 'point)
((eq? m 'info) (list (self 'type) (self 'getx) (self 'gety)) )
((eq? m 'setx!) setx )
((eq? m 'setx!) setx)
((eq? m 'sety!) sety)
((eq? m 'add) add)
(else (display "Message not understood"))))
self)
......@@ -44,15 +47,20 @@
(cond
((null? args) (p m))
(else ((p m) (car args))) ; We have to take the "car" of the args list
(else
(if (procedure? (p m))
((p m) (car args)) ; We have to take the "car" of the args list
; If (p m) is not a procedure, we do nothing and let the error be printed
)
)
)
; If the first parameter, p, is not a procedure, then we return a special error
; If the first parameter, p, is not a procedure, then we return a special error,
; because p is not an appropriate receiver object for the messages.
(display "Inappropriate receiver object") )
)
; Sample code illustrating the execution of the step
; We start by creating two points, p1 and p2
(define p1 (point 1 2))
......@@ -90,6 +98,6 @@
(display (send p 'foo)) ; should display : Message not understood
(newline)
(display (send p 'bar 2)) ; should display : Message not understood, then crash
(display (send p 'bar 2)) ; should display : Message not understood
(newline)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter