Interoperability Macros for roos and racket/  class
1 Macros
send
->
1.1 Comparison with other languages
new
2 Implementation Notes
3 Testing
8.17

Interoperability Macros for roos and racket/class🔗ℹ

Hans Dijkema <hans@dijkewijk.nl>

 (require roos/class) package: roos

This module provides a compatibility layer between the roos object system and the standard racket/class system. It exports the macros send, ->, and new, which automatically dispatch to the appropriate implementation based on the type of the given object or class.

1 Macros🔗ℹ

syntax

send

(send obj method arg ...) A generic message-send macro that works for both Roos objects and standard Racket class objects. If obj is a Roos object (roos-object?), it uses the Roos dispatch (->). Otherwise, it falls back to the original send from racket/class.

Examples:
> (def-roos (t x) this (supers)
    (y x)
    ((f a) (* a x)))
> (define o (new t 5))
> (send o f 2)

10

syntax

->

(-> obj method arg ...) An alternative method dispatch macro. The syntax omits the explicit send and may feel more familiar to users of other programming languages that use concise or operator-based method invocation. This macro checks whether obj is a Roos object or a standard Racket object and dispatches accordingly.

Examples:
> (def-roos (t x) this (supers)
    (y x)
    ((f a) (* a x)))
> (define o (new t 5))
> (-> o f 3)

15

1.1 Comparison with other languages🔗ℹ

In other languages, method invocation often uses compact notation:

The -> macro serves a similar role within the s-expression syntax of Racket.

syntax

new

(new class arg ...) Creates a new object. If class is a Roos class (roos-class?), then roos-new is used. Otherwise, the standard new from racket/class is used, supporting initialization arguments such as (init-field val).

(require roos/class) ; instead of racket/class
 
(define (t% x)
  (class object%
    (init-field (y* x))
    (define/public (y) y*)
    (define/public (y! x) (set! y* x))
    (define/public (f a) (* (send this y) a))
    (super-new)))
 
 
(def-roos (t x) this (supers)
  (y x)
  ((f a) (* (-> this y) a)))
 
 
(displayln
 (let ((cl (t% 5)))
   (let ((o (new cl)))
     (= (send o f 2) 10))))
 
(displayln
 (let ((cl (t% 6)))
   (let ((o (new cl)))
     (= (-> o f 3) 18))))
 
(displayln
 (let ((o (new t 8)))
   (= (-> o f 4) 32)))
 
(displayln
 (= (send (new t 4) f 2) 8))

2 Implementation Notes🔗ℹ

3 Testing🔗ℹ

The module includes an internal test suite using RackUnit. It validates consistent behavior of send, ->, and new across both Racket classes and Roos classes.