GenericVariable

public class GenericVariable<T, I> : VariableProtocol, PatternElement, Equatable where I : Interpreter

Generic superclass of Variables which are aware of their Interpreter classes, as they use it when mapping their values

  • Maps and validates the variable value to another

    Declaration

    Swift

    public typealias VariableMapper<T, I> = (_ body: VariableBody<I>) -> T? where I : Interpreter

    Parameters

    body

    Struct containing the raw matched value and an interpreter object

    Return Value

    The transformed value or nil, if the value was validated with a negative result

  • Unique identifier of the variable that is used when matching and returning them in the matcher.

    Declaration

    Swift

    let name: String
  • Options that modify the behaviour of the variable matching, and the output that the framework provides

    Declaration

    Swift

    let options: VariableOptions
  • map

    The result of the evaluated variable will be running through this map function, transforming its value. By default the map tries to convert the matched value to the expected type, using the as? operator.

    Declaration

    Swift

    let map: VariableMapper<T, I>
  • Initialiser for all the properties

    Declaration

    Swift

    public init(_ name: String,
                options: VariableOptions = [],
                map: @escaping VariableMapper<T, I> = { $0.value as? T })

    Parameters

    name

    GenericVariables have a name (unique identifier), that is used when matching and returning them in the matcher.

    options

    Options that modify the behaviour of the variable matching, and the output that the framework provides

    map

    If provided, then the result of the evaluated variable will be running through this map function. By default the map tries to convert the matched value to the expected type, using the as? operator. Defaults to identical map, using the as? operator for value transformation

  • GenericVariables always return anyMatch MatchResult, forwarding the shortest argument, provided during initialisation

    Declaration

    Swift

    public func matches(prefix: String, options: PatternOptions = []) -> MatchResult<Any>

    Parameters

    prefix

    The input

    Return Value

    The result of the match operation. Always anyMatch with the shortest argument, provided during initialisation

  • A helper method to map the value of the current variable to another type

    Declaration

    Swift

    public func mapped<K>(_ map: @escaping (_ value: T) -> K?) -> GenericVariable<K, I>

    Parameters

    map

    The transformation function

    value

    The value to be mapped

    Return Value

    A new variable instance using the value mapper block

  • The result of the evaluated variable will be ran through this map function, transforming its value. By default the map tries to convert the matched value to the expected type, using the as? operator.

    Declaration

    Swift

    func performMap(input: Any, interpreter: Any) -> Any?

    Parameters

    input

    The first parameter is the value is going to be transformed

    interpreter

    Helps the mapper function to parse and interpret the contents

    Return Value

    The transformed value or nil - if the value was validated with a negative result

  • Declaration

    Swift

    public static func == (lhs: GenericVariable<T, I>, rhs: GenericVariable<T, I>) -> Bool