case-kw-lambda
case-kw-lambda
case-kw->
8.12

case-kw-lambda🔗ℹ

Alexis King <lexi.lambda@gmail.com>

The source of this manual is available on GitHub.

This package provides the case-kw-lambda form, a variant of case-lambda that has been extended to support keyword arguments and optional arguments like lambda. Additionally, it provides the case-kw-> contract combinator, which extends case-> with support for keyword arguments and ->* subcontracts.

syntax

(case-kw-lambda
  [kw-formals body ...+] ...)
 
kw-formals = (arg ...)
  | (arg ...+ . rest-id)
  | rest-id
     
arg = id
  | [id default-expr]
  | keyword id
  | keyword [id default-expr]
Produces a procedure. Each [kw-formals body ...] clause is analogous to a single lambda procedure; applying the case-kw-lambda-generated procedure is the same as applying a procedure that corresponds to one of the clauses—the first procedure that accepts the given arguments. If no corresponding procedure accepts the given arguments, an exn:fail:contract:arity exception is raised.

Note that, unlike case-lambda, a case-kw-lambda clause supports the full kw-formals of lambda, including keyword and optional arguments.

Examples:
> (define distance
    (case-kw-lambda
      [(dx dy)
       (distance #:dx dx #:dy dy)]
      [(#:dx dx #:dy dy)
       (sqrt (+ (expt dx 2) (expt dy 2)))]))
> (distance 3 4)

5

> (distance #:dx 5 #:dy 12)

13

> (distance 8 #:dy 15)

application: no case matching 1 non-keyword argument

  procedure: distance

  arguments...:

   8

   #:dy 15

syntax

(case-kw-> arr-ctc ...)

 
arr-ctc = (-> dom ... range)
  | (-> dom ... dom-expr ellipsis dom ... range)
  | (->* [dom ...] optional-doms rest range)
     
dom = dom-expr
  | keyword dom-expr
     
optional-doms = 
  | [dom ...]
     
rest = 
  | #:rest rest-expr
     
range = range-expr
  | (values range-expr ...)
  | any
     
ellipsis = ...
Produces an contract intended to match case-kw-lambda. Each arr-ctc is a contract that governs a clause in the case-kw-lambda; its grammar matches that of -> and ->*.