diff options
author | Eli Barzilay <eli@barzilay.org> | 2013-07-16 03:44:26 -0400 |
---|---|---|
committer | Eli Barzilay <eli@barzilay.org> | 2013-07-16 03:44:26 -0400 |
commit | c99ac1be7ea81d275564b4a4ae573a45f017e69d (patch) | |
tree | b75f46699739e6ee743e1b0ef5fc137be0d45503 /racket.html.markdown | |
parent | 8f295f4e6893620181270a35a22d19926d98f626 (diff) |
Improve class section.
Diffstat (limited to 'racket.html.markdown')
-rw-r--r-- | racket.html.markdown | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/racket.html.markdown b/racket.html.markdown index ab7d3a69..006d6b86 100644 --- a/racket.html.markdown +++ b/racket.html.markdown @@ -445,7 +445,7 @@ vec ; => #(1 2 3 4) ;; 8. Classes and Objects ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Create a class fish% +;; Create a class fish% (-% is idomatic for class bindings) (define fish% (class object% (init size) ; initialization argument @@ -465,9 +465,23 @@ vec ; => #(1 2 3 4) (new fish% [size 10])) ;; Use `send' to call an object's methods +(send charlie get-size) ; => 10 (send charlie grow 6) (send charlie get-size) ; => 16 +;; `fish%' is a plain "first class" value, which can get us mixins +(define (add-color c%) + (class c% + (init color) + (super-new) + (define my-color color) + (define/public (get-color) my-color))) +(define colored-fish% (add-color fish%)) +(define charlie2 (new colored-fish% [size 10] [color 'red])) +(send charlie2 get-color) +;; or, with no names: +(send (new (add-color fish%) [size 10] [color 'red]) get-color) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; 9. Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |