[top][index]
search for:

OptionTable ==> Function -- attaching options to a function

Synopsis:

  • Usage: g = defs ==> fun
  • Operator: ==> -- a binary operator
  • Input:
  • defs, an instance of class OptionTable: a hash table whose keys are the names of the optional arguments, and whose values are the corresponding default values.
  • fun, an instance of class Function: a function that expects optional arguments.
  • Output:
  • g, an instance of class Function: a new function that pre-processes the optional arguments and then calls fun.
  • The new function g works as follows. The value of g args, say, is obtained by evaluation of (fun opts)(args'), where args' is obtained from args by removing the options of the form X=>A (where X is a name of an optional argument), and opts is a hash table of the same form as defs in which the default values have been replaced by the user-supplied values, e.g., the value stored under the key X has been replaced by A.

    Remark: defs can also be simply a list of options.

    In the following example we use a simple definition for fun so we can see everything that fun receives.

    i1 : g = {a=>1, b=>2} ==> opts -> args -> {args, opts}

    o1 = g

    o1 : Function
    i2 : g x

    o2 = {x, OptionTable{a => 1}}
                         b => 2

    o2 : List
    i3 : g(x,y,b=>66)

    o3 = {(x, y), OptionTable{a => 1 }}
                              b => 66

    o3 : List
    i4 : g(t,u,a=>44,b=>77)

    o4 = {(t, u), OptionTable{a => 44}}
                              b => 77

    o4 : List

    See also:

  • making new functions with optional arguments
  • OptionTable -- the class of hash tables for optional arguments
  • Option -- the class of all pairs x => y
  • => -- produce an Option
  • Code:

         -- ../../../Macaulay2/m2/option.m2:31-38
           (defaults,f) -> (
              args -> (
                -- Common code for functions created with ==> to
                -- process options and arguments.
                f ## override (defaults,args)
                )
              )
           )

    [top][index]
    search for: