Am Sonntag, 24. April 2016 19:29:31 UTC+3 schrieb Matthias Felleisen:
> > On Apr 24, 2016, at 9:05 AM, Daniel Karch <[email protected]> wrote:
> > 
> > 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?
> 
> 
> 
> Typed Racket does not support ML's abstract types. The goal is primarily to 
> allow Racket programmers to port existing programs to the typed world in an 
> incremental fashion. While I consider the existing system usable, there are 
> many open questions and, frankly, some of them are open research questions, 
> including how to add expressive power to Typed Racket’s type language that 
> works with Racket programs. 
> 
> Typed Racket also doesn’t support Racket’s interfaces yet, another open 
> research topic. 
> 
> What you have is as close as you can get to what you ask for. Alternatively 
> experiment with structures (but you seem to come from the OO world, so the 
> above is good)
> 
> — Matthias

Hello Matthias,

thank you for the explanation. I wouldn't say that I come from the OO world – I 
would much prefer to keep data and functions separate, but I have literally one 
day of experience with Typed Racket, so the above class-based version was the 
only way I could get it to work so far. If you could point me towards some 
resources on how this could be done with structures, that would be great.

Best regards 

  Daniel

-- 
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