DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH
 

(guile.info.gz) Why Use Keywords?

Info Catalog (guile.info.gz) Keywords (guile.info.gz) Coding With Keywords
 
 21.7.1 Why Use Keywords?
 ------------------------
 
 Keywords are useful in contexts where a program or procedure wants to be
 able to accept a large number of optional arguments without making its
 interface unmanageable.
 
    To illustrate this, consider a hypothetical `make-window' procedure,
 which creates a new window on the screen for drawing into using some
 graphical toolkit.  There are many parameters that the caller might
 like to specify, but which could also be sensibly defaulted, for
 example:
 
    * color depth - Default: the color depth for the screen
 
    * background color - Default: white
 
    * width - Default: 600
 
    * height - Default: 400
 
    If `make-window' did not use keywords, the caller would have to pass
 in a value for each possible argument, remembering the correct argument
 order and using a special value to indicate the default value for that
 argument:
 
      (make-window 'default              ;; Color depth
                   'default              ;; Background color
                   800                   ;; Width
                   100                   ;; Height
                   ...)                  ;; More make-window arguments
 
    With keywords, on the other hand, defaulted arguments are omitted,
 and non-default arguments are clearly tagged by the appropriate
 keyword.  As a result, the invocation becomes much clearer:
 
      (make-window #:width 800 #:height 100)
 
    On the other hand, for a simpler procedure with few arguments, the
 use of keywords would be a hindrance rather than a help.  The primitive
 procedure `cons', for example, would not be improved if it had to be
 invoked as
 
      (cons #:car x #:cdr y)
 
    So the decision whether to use keywords or not is purely pragmatic:
 use them if they will clarify the procedure invocation at point of call.
 
Info Catalog (guile.info.gz) Keywords (guile.info.gz) Coding With Keywords
automatically generated byinfo2html