(define-module (data optional) #:use-module (oop goops) #:use-module (ice-9 match) #:use-module (control monad) #:export (nothing just nothing? just? return-optional) ;; TODO is this reexport needed? #:re-export (>>=)) (define-class () (slot #:init-value #f #:init-keyword #:slot) (just #:init-value #t #:init-keyword #:just)) (define (nothing) (make #:just #f)) (define (just obj) (make #:just #t #:slot obj)) (define (nothing? this) (not (slot-ref this 'just))) (define (just? this) (not (nothing? this))) (define-method (write (this ) port) (if (just? this) (format port "[Just ~s]" (slot-ref this 'slot)) (format port "[Nothing]"))) (define return-optional just) (define-method (>>= (this ) (proc )) (cond ((nothing? this) (nothing)) ((just? this) (match this (($ slot) (proc slot))))))