Callable.function constructor

Callable.function(
  1. String name,
  2. String arguments,
  3. Value callback(
    1. List<Value> arguments
    )
)

Creates a function with the given name and arguments that runs callback when called.

The argument declaration is parsed from arguments, which uses the same syntax as an argument list written in Sass (not including parentheses). The arguments list may be empty to indicate that the function takes no arguments. Arguments may also have default values. Throws a SassFormatException if parsing fails.

Any exceptions thrown by callback are automatically converted to Sass errors and associated with the function call.

For example:

new Callable.function("str-split", r'$string, $divider: " "',
    (arguments) {
  var string = arguments[0].assertString("string");
  var divider = arguments[1].assertString("divider");
  return new SassList(
      string.value.split(divider.value).map((substring) =>
          new SassString(substring, quotes: string.hasQuotes)),
      ListSeparator.comma);
});

Functions may also take variable length argument lists. These are declared the same way as in Sass, and are passed as the final argument to the callback. For example:

new Callable.function("str-join", r'$strings...', (arguments) {
  var args = arguments.first as SassArgumentList;
  var strings = args.map((arg) => arg.assertString()).toList();
  return new SassString(strings.map((string) => string.text).join(),
      quotes: strings.any((string) => string.hasQuotes));
});

Note that the argument list is always an instance of SassArgumentList, which provides access to keyword arguments using SassArgumentList.keywords.

Implementation

factory Callable.function(String name, String arguments,
        Value callback(List<Value> arguments)) =>
    BuiltInCallable.function(name, arguments, callback);