Created
January 29, 2015 15:59
-
-
Save mnzk/6c8d90802136c6df42c7 to your computer and use it in GitHub Desktop.
send と send-generic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define sprite<%> (interface () draw)) | |
(define draw (generic sprite<%> draw)) | |
(define pix% | |
(class* object% (sprite<%>) | |
(super-new) | |
(define/public (draw) | |
'pix-draw))) | |
(define tex% | |
(class* object% (sprite<%>) | |
(super-new) | |
(define/public (draw) | |
'tex-draw))) | |
;; interface sprice<%>をimplせずにdrawを実装 | |
(define hoge% | |
(class object% | |
(super-new) | |
(define/public (draw) | |
'hoge-draw))) | |
(define ls (list (new tex%) (new pix%) (new hoge%))) | |
;; OK メソッド名だけでディスパッチ (ダックタイピング) | |
(map (lambda (obj) (send obj draw)) | |
ls) | |
;;=> '(tex-draw pix-draw hoge-draw) | |
;; Type Error | |
;; 同じメソッド名でも、型が違えばエラー | |
(map (lambda (obj) | |
(send-generic obj draw) ) | |
ls) | |
;;=> generic:draw: target is not an instance of the generic's interface | |
;; target: (object:hoge% ...) | |
;; interface name: sprite<%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment