From 2bc0d1c15de43be99e2cdb7f5ed2c1d56e2a6417 Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Sat, 13 Jul 2024 08:25:15 +0100 Subject: [PATCH] Add NumericExpression typealias --- README.md | 13 +++++++++++++ Sources/Expression.swift | 4 ++++ Tests/ExpressionTests.swift | 2 ++ 3 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 120b859..5578b82 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Normally these kind of calculations would involve embedding a heavyweight interp Expression is fast, lightweight, well-tested, and written entirely in Swift. It is substantially faster than using JavaScriptCore for evaluating simple expressions (see the [Benchmark](#benchmark) app for a scientific comparison. + ## How? Expression works by parsing an expression string into a tree of symbols, which can then be evaluated at runtime. Each symbol maps to a Swift closure (function) which is executed during evaluation. There are built-in functions representing common math operations, or you can provide your own custom ones. @@ -108,6 +109,18 @@ To install using Swift Package Manager, add this to the `dependencies:` section ## Integration +To start using Expression, import the Expression module at the top of your file: + +```swift +import Expression +``` + +**Note:** In iOS 18 / macOS 15 Apple added a `Foundation.Expression` class that clashes with the `Expression` defined in the Expression library if you are importing Foundation in your file. To work around this, use the `NumericExpression` alias instead. Or if you prefer, you can override Apple's `Expression` with `NumericExpression` locally in your project by writing: + +```swift +typealias Expression = NumericExpression +``` + You create an `Expression` instance by passing a string containing your expression, and (optionally) any or all of the following: * A set of configuration options - used to enabled or disable certain features diff --git a/Sources/Expression.swift b/Sources/Expression.swift index 8d0abc5..0da4fba 100644 --- a/Sources/Expression.swift +++ b/Sources/Expression.swift @@ -34,6 +34,10 @@ import Dispatch import Foundation +/// Alternative name to use for Expression if you need to disambiguate from the +/// `Foundation.Expression` type introduced in iOS 18 / macOS 15 +public typealias NumericExpression = Expression + /// Immutable wrapper for a parsed expression /// Reusing the same Expression instance for multiple evaluations is more efficient /// than creating a new one each time you wish to evaluate an expression string diff --git a/Tests/ExpressionTests.swift b/Tests/ExpressionTests.swift index c4a8af9..72e1f7e 100644 --- a/Tests/ExpressionTests.swift +++ b/Tests/ExpressionTests.swift @@ -32,6 +32,8 @@ @testable import Expression import XCTest +typealias Expression = NumericExpression + class ExpressionTests: XCTestCase { // MARK: Description