Custom Functions

Define Swift implementations of Sass @functions by declaring a SassFunctionMap and passing it to the compiler.

Use the SassValue family of types within the function to exchange data with the Sass compiler.

A set of functions can be scoped either to an individual compilation or to all a compiler’s compilations.


Topics

typealias SassFunction

The Swift type of a Sass function.

Use this to define Sass functions written in Swift that are available to stylesheets as they are compiled.

SassValue is used to transmit function parameters and return values: any parameters with default values are instantiated before the function is called. If your function signature includes an argument list such as funcName($arg1, $others...) then the args list is passed as a list value, so in the example the function receives two arguments: one for $arg1 and one for whatever else is passed.

Usually the first job of a function is to downcast each parameter to the expected type using throwing methods such as SassValue.asString().

Any error thrown from the function ends up cancelling the compilation job and returning an error message back to the user that contains the text of the error thrown.

All Sass functions return a value – there is no void return type. Create new SassValue objects using a subclass initializer such as SassString.init(_:isQuoted:).

Declaration
Swift
public typealias SassFunction = ([SassValue]) async throws -> SassValue

typealias SassFunctionSignature

A Sass function signature.

This is text that can appear after @function in a Sass stylesheet, such as mix($color1, $color2, $weight: 50%).

Declaration
Swift
public typealias SassFunctionSignature = String

typealias SassFunctionMap

A set of SassFunctions and their signatures.

Do not include two function signatures that have the same function name, such as myFunc($a) and myFunc($b): the program will terminate.

Declaration
Swift
public typealias SassFunctionMap = [SassFunctionSignature: SassFunction]