[plt-scheme] Limiting access to the parent class in Swindle
On May 1, Paulo Jorge de Oliveira Cantante de Matos wrote:
> ;; Everyclass will inherit from math
> (defclass math ())
>
> ;; Class Number
> (defclass number (math))
>
> ;; Class Rational
> (defclass rational (number)
> (numerator :accessor rational-numerator :initarg numerator)
> (denominator :accessor rational-denominator :initarg
> denominator))
>
> ;; Class Integer
> (defclass integer (rational)
> (denominator :reader rational-denominator
> :allocation :class
> :initvalue 1))
>
>
> (defmethod (print-object (obj integer) esc? port)
> (fprintf port "~a" (rational-numerator obj)))
>
>
> > (make integer :numerator 2)
> #<struct:swindleobj>
> > (rational-numerator (make integer :numerator 2))
> #<undefined>
> >
>
> Any idea why print-object is not printing the object and why the second
> interaction doesn't return 2?
1. Use a keyword for :initarg. When you write :initarg numerator
above, you get the Scheme built-in numerator function as the
keyword, which is most likely not what you intended.
2. Use class names that look like <foo>, especially when you define
things that are so close to built-in functions. In your case this
will still be problematic because Swindle already has classes like
<number> and <rational>.
3. You don't need to define a rational-denominator again for the
integer class. Looks like you think that this will remove the
rational-denominator setter, but it doesn't. Instead, you probably
want to redefine the set-rational-denominator! method (that was
created by the rational class definition ) for integer objects to
raise an error.
4. Again, this does not protect you against changing the value through
slot-set!.
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!