SassScript

SassScript is the Sass expression language. Understand the arguments passed to SassFunctions and return values using these types. The behaviors and functionality here match that in Sass itself.


Topics

Main types

class SassValue

Common behavior between values passed to or returned from Sass functions.

All Sass values can be treated as lists: singleton values like strings behave like 1-element lists and maps behave like lists of two-element (key, value) lists. Use the SequenceSequence conformance to access the list contents.

SassValue is abstract, you cannot create instances. Instead create instances of subtypes like SassColor.

All the SassValue types have value semantics and are almost all entirely immutable: the only mutability is to do with caching of derived properties.

Declaration
Swift
public class SassValue: Hashable, Sequence, CustomStringConvertible, @unchecked
    Sendable

class SassString

A Sass string value.

Strings may be quoted.

SassString indexes

Sass models strings as a sequence of unicode scalars, not Swift’s primary view as a sequence of extended grapheme clusters. So any string index you receive through Sass applies to the unicode scalar view of the string.

Further, Sass models 1 as the index of the first scalar in the string, and count as the index of the last. This class offers a scalarIndexFrom(sassIndex:) method to wrap up both parts of this conversion, but offers only sympathy to users having to deal with the results.

SassString conforms to SequenceSequence via SassValue. This sequence is a singleton sequence containing the string value, not a sequence of scalars.

Declaration
Swift
public final class SassString: SassValue

class SassNumber

A Sass numeric value.

Sass numbers are DoubleDoubles with units.

Sass has its own rules for numeric equality and integer conversion that use only the first 10 decimal places. Use the SassNumber methods to convert to integers and test for ranges.

The units on a SassNumber can describe the result of multiplying and dividing numbers with primitive units. Best described in the Sass docs. The CSS values spec defines several common units and how to convert between them, for example px to pt. asConvertedTo(...) implements these conversions which lets you, for example, write a function that accepts any length unit that you can easily convert to your preferred unit.

Because of units, SassNumber is not ComparableComparable — there is no ordering relation possible between “5 cm” and “12 kHz”. It is EquatableEquatable though respecting unit conversion so that “1 cm” == “10 mm”.

Declaration
Swift
public final class SassNumber: SassValue

class SassColor

A Sass color value.

Color values are originally defined using either the RGB-A, HSL-A, or HWB-A color model, but can be accessed as any: conversions are performed and cached internally.

note

Parameter values follow web standards rather the Apple SDK standards, so for example ‘red’ is modelled as an integer in 0…255.

Declaration
Swift
public final class SassColor: SassValue

enum SassConstants

SassScript constant values.

Declaration
Swift
public enum SassConstants

class SassList

A Sass list value.

Sass lists have a separator and may be surrounded with brackets. All Sass values can be treated as lists so much list-like behavior is available via SassValue. SassList is mostly useful for constructing your own multi-element lists.

Declaration
Swift
public class SassList: SassValue

class SassArgumentList

A Sass value representing a $arg... function argument list.

This type is a SassList holding positional arguments with an addition set of named keyword arguments. See the Sass docs.

warning

You’ll typically use this type to access arguments passed in this way to a custom Sass function. Be careful in other scenarios: the keyword argument part of the type is excluded from equality and listification, meaning it is easy to accidentally lose that part of the type when passing instances through generic SassValue code.

Declaration
Swift
public final class SassArgumentList: SassList

class SassMap

A Sass map value.

Sass maps are DictionaryDictionary-like collections. The keys can be any kind of SassValue.

When a map is viewed as a Sass list then the list is of two-element lists, one for each key-value pair in the map. The pairs are in no particular order.

Declaration
Swift
public final class SassMap: SassValue

class SassCalculation

A Sass calculation value.

These correspond to Sass calc(), min(), max(), or clamp() expressions. See the Sass docs.

The Sass compiler simplifies these before sending them to custom functions: this means that if you receive a SassCalculation argument then it cannot be further simplified at compile time, for example calc(20px + 30%).

The API here allows you to construct SassCalculations representing calc()-type expressions including invalid ones such as calc(20px, 30px) as though you were writing a stylesheet. The validity is checked – and the overall expression simplified – by the compiler when it receives the value.

Declaration
Swift
public final class SassCalculation: SassValue

class SassMixin

A Sass mixin.

Values representing mixins can only be created by the compiler. See the Sass docs.

Declaration
Swift
public final class SassMixin: SassValue

Function object types

class SassCompilerFunction

A Sass compiler function.

A compiler function is an opaque function defined by the Sass compiler that can be passed as an argument to or returned by a SassFunction.

Right now there is no way to explicitly request they be executed; all you can do with this type is validate that it appears when you expect it to and pass it back to the compiler when needed.

Declaration
Swift
public final class SassCompilerFunction: SassValue

class SassDynamicFunction

A dynamic Sass function.

These are Sass functions, written in Swift, that are not declared up-front to the compiler when starting compilation. Instead they are returned as SassValues from other SassFunctions (that were declared up-front) so the compiler can call them later on.

Declaration
Swift
public final class SassDynamicFunction: SassValue

Less-commonly used types

protocol SassValueVisitor

A protocol for implementing polymorphic operations over SassValue objects.

Declaration
Swift
public protocol SassValueVisitor

class SassNull

The Sass null value.

You cannot create instances of this type: use SassConstants.null instead.

Declaration
Swift
public final class SassNull: SassValue

class SassBool

A Sass boolean value.

You cannot create instances of this type: use SassConstants.true and SassConstants.false instead.

Declaration
Swift
public final class SassBool: SassValue