[racket] How to use abstract classes?

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Wed Aug 24 16:54:46 EDT 2011

On 2011-08-24 10:31:00 -0400, Alexander Kasiukov wrote:
> 
> What is the best way to make a class in Racket abstract? I would
> like to have a class which

In addition to all the approaches so far, you can also use contracts to
keep a class from being instantiated even when you export it.

  #lang racket/load

  ;; cannot instantiate base% outside of this module
  (module a racket
    (define base%
      (class object%
        (super-new)
        (init abstract)
        (define/public (common)
          (displayln "noop"))))
 
    (define sub%
      (class base%
        ;; this works here
        (super-new [abstract "dummy"])))
  
    (provide/contract
      [sub% (class/c)]
      [base% (class/c (init [abstract none/c]))]))
  
  (require 'a)
  
  (send (new sub%) common)                     ; ok
  (new base% [abstract 0])                     ; contract error
  (new (class base% (super-new [abstract 0]))) ; contract error

This doesn't let you subclass outside of the module though because of
the contract. You can still use the class value for is-a? or subclass?
checks, though it may be better to use interfaces if you need that
anyway.

Cheers,
Asumu


Posted on the users mailing list.