Skip to content

Commit

Permalink
Add NumericExpression typealias
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jul 14, 2024
1 parent cd5c7d2 commit 2bc0d1c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Sources/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Tests/ExpressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
@testable import Expression
import XCTest

typealias Expression = NumericExpression

class ExpressionTests: XCTestCase {
// MARK: Description

Expand Down

0 comments on commit 2bc0d1c

Please sign in to comment.