On this page:
variant?
variant
variant-value
variant-tag
variant-tagged-as?
7.5

1.8 Variants

 (require rebellion/base/variant) package: rebellion

A variant is a value tagged with a keyword. Variants are used to distinguish different kinds of values by name, without knowing anything about the types of those values.

procedure

(variant? v)  variant?

  v : any/c
A predicate for variants.

procedure

(variant #:<kw> v)  variant?

  v : any/c
Constructs a variant containing v tagged with the given keyword, where #:<kw> stands for any keyword.

Examples:
> (variant #:success 42)

(variant #:success 42)

> (variant #:failure "oops")

(variant #:failure "oops")

Additionally, the variant constructor may be used as a match expander.

Examples:
(define (try-add1 v)
  (match v
    [(variant #:success x) (add1 x)]
    [(variant #:failure msg) (error msg)]))

 

> (try-add1 (variant #:success 42))

43

> (try-add1 (variant #:failure "oops"))

oops

procedure

(variant-value var)  any/c

  var : variant?
Returns the value contained in var.

Examples:
> (variant-value (variant #:success 42))

42

> (variant-value (variant #:failure "oops"))

"oops"

procedure

(variant-tag var)  keyword?

  var : variant?
Returns the tag keyword of var.

Examples:
> (variant-tag (variant #:success 42))

'#:success

> (variant-tag (variant #:failure "oops"))

'#:failure

procedure

(variant-tagged-as? var tag-keyword)  boolean?

  var : variant?
  tag-keyword : keyword?
Returns #t if var is tagged with tag-keyword, returns #f otherwise.

Examples:
> (variant-tagged-as? (variant #:success 42) '#:success)

#t

> (variant-tagged-as? (variant #:success 42) '#:failure)

#f