For a project I am creating a class driver%
which is supposed to be a layer of abstraction over different modules providing the same procedures. The class would be initialized with an argument that specifies the module to be used.
Additionally, I would like driver%
to expose the same procedures but without side effects, to facilitate unit testing classes that use the driver.
Consider the following:
module_one.rkt
#lang racket
(provide foo)
(define (foo)
(display "called from "module one"))
module_two.rkt
#lang racket
(provide foo)
(define (foo)
(display "called from "module two"))
driver.rkt
#lang racket
(require (prefix-in one: "module_one.rkt")
(prefix-in two: "module_two.rkt"))
(provide driver%)
(define driver%
(class object%
(super-new)
(init driver-choice)
(define choice driver-choice)
(define/public (foo)
(case choice
[(1) (one:foo)]
[(2) (two:foo)]
[else void]))))
This fulfills the above requirements, but is not very elegant: for each exposed procedure, another case expression will have to be added. This seems unnecessary, as the choice for an API is made when the class is instantiated, so the choice will be the same everywhere.
What would be a more acceptable solution to this problem? I have looked into using local-require
, but this does not seem to work with define/public
.
Many thanks!