Anters Bear
Anters Bear

Reputation: 1956

Cannot assign through subscript: subscript is get-only

I'm using Expression to create a very advanced calculator that takes string input from a user. I'm trying to add functions to give the calculator more features however I am not able to add custom or cast functions. Could you kindly explain me why the I am getting the following error.

Code that works

 public static let mathSymbols: [Symbol: SymbolEvaluator] = {
        var symbols: [Symbol: SymbolEvaluator] = [:]
        symbols[.function("atan", arity: 1)] = { atan($0[0]) }
        symbols[.function("abs", arity: 1)] = { abs($0[0]) }
        symbols[.function("log", arity: 1)] = { log2($0[0]) }
        return symbols
 }()

Input that doesnt work

        symbols[.function("number", arity: 1)] = { Float($0[0]) }
        symbols[.function("number", arity: 1)] = { someFunction($0[0]) }

Error

Cannot assign through subscript: subscript is get-only

Upvotes: 2

Views: 2498

Answers (1)

Martin R
Martin R

Reputation: 540085

The error message is misleading. Expression.swift defines

public typealias SymbolEvaluator = (_ args: [Double]) throws -> Double

which means that the closure must return a Double:

symbols[.function("number", arity: 1)] = { Double($0[0]) }

Upvotes: 1

Related Questions