Skip to content

Instantly share code, notes, and snippets.

@mnzk
Created January 29, 2015 15:59
Show Gist options
  • Save mnzk/6c8d90802136c6df42c7 to your computer and use it in GitHub Desktop.
Save mnzk/6c8d90802136c6df42c7 to your computer and use it in GitHub Desktop.
send と send-generic
#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