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 : InterpreterParameters
bodyStruct 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 -
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
nameGenericVariables have a name (unique identifier), that is used when matching and returning them in the matcher.optionsOptions that modify the behaviour of the variable matching, and the output that the framework provides
mapIf 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 theas?operator for value transformation -
GenericVariablesalways return anyMatch MatchResult, forwarding the shortest argument, provided during initialisationDeclaration
Swift
public func matches(prefix: String, options: PatternOptions = []) -> MatchResult<Any>Parameters
prefixThe input
Return Value
The result of the match operation. Always
anyMatchwith 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
mapThe transformation function
valueThe 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
inputThe first parameter is the value is going to be transformed
interpreterHelps 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
View on GitHub
Install in Dash
GenericVariable Class Reference