You can specify the arguments to a function by name. This can be useful
when you need to add occasionally-used parameters to existing functions,
similar to the way you can use default parameter values in languages like
C++.
define function describe-list(my-list :: <list>, #key verbose?) => () format(*standard-output*, "{a <list>, size: %d", my-list.size); if (verbose?) format(*standard-output*, ", elements:"); for (item in my-list) format(*standard-output*, " %=", item); end for; end if; format(*standard-output*, "}"); end function;This method could be invoked in one of several ways. The first
specifies no keyword arguments, and exhibits the default behavior:describe-list(#(1, 2, 3, 4, 5, 6));
// prints “{a <list>, size: 6}”Alternatively, the verbose? keyword argument could
be used to indicate the ‘verbose’ behavior was desired:describe-list(#(5, 7, 3), verbose?: #t);
// prints “{a <list>, size: 3, elements: 5 7 3}”
Dylan’s format
statement works just like
printf
, except that you can extend it to handle new types of
objects.