From c99ac1be7ea81d275564b4a4ae573a45f017e69d Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Tue, 16 Jul 2013 03:44:26 -0400 Subject: Improve class section. --- racket.html.markdown | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'racket.html.markdown') 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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- cgit v1.2.3