version-case : conditionally compile code based on current version number.

_version-case_ : conditionally compile code based on current version number.

Danny Yoo ( /                


(module some-sample-unit-code mzscheme
  (require (planet "" ("dyoo" "version-case.plt" 1 1))
           (lib "" "mred"))
  ;; Small test code to see that we can write unit-dependent code
  ;; that still runs under both 360 and 369.
   [(version<= (version) "360")
    (printf "old unit code~n")
    (require (lib "" "drscheme")
             (lib ""))
    (define tool@
      (unit/sig drscheme:tool-exports^
        (import drscheme:tool^)
        (define (phase1)
          (message-box "phase1"))
        (define (phase2)
          (message-box "phase2"))))]
    (printf "new unit code~n")
    (require (lib "" "drscheme")
             (lib ""))
    (define-unit tool@
      (import drscheme:tool^)
      (export drscheme:tool-exports^)
      (define (phase1)
        (message-box "phase1"))
      (define (phase2)
        (message-box "phase2")))]))


_version-case_ : SYNTAX

    (version-case [test code ...]
                  [else code ...])

Expands out to one of the CODE blocks, depending on which test succeeds first.
The test expression has access to the mzscheme primitives.  In addition,
some version-comparing functions, are available for convenience.

    ;; version<=: string string -> boolean
    ;; Returns true if the strings, treated as version strings "major.minor",
    ;; compare as <=.

    ;; version>=: string string -> boolean
    ;; version=: string string -> boolean
    ;; version<: string string -> boolean
    ;; version>: string string -> boolean

Bugs / Gotchas

The tests are done at compile time, in the standard mzscheme environment built with
(make-namespace).  Also, if the test itself raises exceptions, the error messages
aren't that good.