Hi,

I recently started learning Racket and like it so far. Since I very much prefer 
statically typed languages, I am leaning towards Typed Racket.

In a small program I am writing I would like to have a function that accepts a 
value of an abstract type and then calls a function on that type whose 
implementation depends on the concrete type. I.e., what would be an interface 
in Java with several classes implementing it or a type class in Haskell with 
several instances. 
In vanilla Racket it seems that I could use generic interfaces or class-based 
interfaces, but in TR neither of these options seems to work. The following 
works:

#lang typed/racket

;; "Interface"
(define-type X
  (Object [get-name (-> String)]))

;; Implementation 1
(define A%
  (class object%
    (super-new)
    (init-field [name : String])
    (: get-name (-> String))
    (define/public (get-name)
      (~a "I am an A% and my name is " name "."))))

;; Implementation 2
(define B%
  (class object%
    (super-new)
    (init-field [name : String])
    (: get-name (-> String))
    (define/public (get-name)
      (~a "I am a B% and my name is " name "."))))

(: display-name (-> X Void))
(define (display-name x)
  (displayln (send x get-name)))

(display-name (make-object A% "Alice")) ;; -> I am an A and my name is Alice.
(display-name (make-object B% "Bob"))   ;; -> I am a B and my name is Bob.

– but I am not completely happy about it. Is there an idiomatic way to achieve 
what I described in TR?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to