From: Hillary Ryan (hillaryryan92 at gmail.com) Date: Tue Jun 12 12:03:07 EDT 2012 |
|
Hello, Disclaimer: I am new to Racket, so please forgive the ignorance. Questions: 1) As a functional language, when Racket implemented classes did it do so with a different perspective or design philosophy than procedural languages that made a similar transition? In other words, what is the correct, Rackety way to think about and to use classes? 2) Reading through the documentation it seems that there are three different ways to update fields: Way 1: (class<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._class))> object%<http://docs.racket-lang.org/reference/createclass.html#(def._((lib._racket/private/class-internal..rkt)._object~25))> (init<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._init))> size) ; initialization argument (define<http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))> current-size size) ; field (super-new<http://docs.racket-lang.org/reference/objcreation.html#(form._((lib._racket/private/class-internal..rkt)._super-new))> ) ; superclass initialization (define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))> (get-size) current-size) (define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))> (grow amt) (set!<http://docs.racket-lang.org/reference/set_.html#(form._((quote._~23~25kernel)._set!))> current-size (+<http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._+))> amt current-size))) (define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))> (eat other-fish) (grow (send<http://docs.racket-lang.org/reference/ivaraccess.html#(form._((lib._racket/private/class-internal..rkt)._send))> other-fish get-size)))) Way 2: (define<http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))> account% (class<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._class))> object%<http://docs.racket-lang.org/reference/createclass.html#(def._((lib._racket/private/class-internal..rkt)._object~25))> (super-new<http://docs.racket-lang.org/reference/objcreation.html#(form._((lib._racket/private/class-internal..rkt)._super-new))> ) (init-field<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._init-field))> balance) (define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))> (add n) (new<http://docs.racket-lang.org/reference/objcreation.html#(form._((lib._racket/private/class-internal..rkt)._new))> this%<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._this~25))> [balance (+<http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._+))> n balance)])))) Way 3: (define<http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))> savings% (class<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._class))> account% (super-new<http://docs.racket-lang.org/reference/objcreation.html#(form._((lib._racket/private/class-internal..rkt)._super-new))> ) (inherit-field<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._inherit-field))> balance) (define<http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))> interest 0.04) (define/public<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._define/public))> (add-interest) (send<http://docs.racket-lang.org/reference/ivaraccess.html#(form._((lib._racket/private/class-internal..rkt)._send))> this<http://docs.racket-lang.org/reference/createclass.html#(form._((lib._racket/private/class-internal..rkt)._this))> add (*<http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._*))> interest balance))))) How do these approaches differ? And when should I use one over the other? Many Thanks, Hillary Ryan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.racket-lang.org/users/archive/attachments/20120612/b99af8d6/attachment-0001.html>
Posted on the users mailing list. |
|