Skip to content

Commit 5dfc460

Browse files
Merge pull request #97 from stephentyrone/prefix-modules
Enable name lookup for modules by suffixing `Module` when needed to break ambiguity. In particular, the following module renames are applied: - Real -> RealModule - Complex -> ComplexModule This is a source-breaking change, if you currently directly import these modules. (sorry!) The fix is to either import Numerics instead, or to use the new, suffixed, names. The good news is that fixing this should require only a straightforward search-and-replace for anyone affected.
2 parents 119377c + 014ce56 commit 5dfc460

23 files changed

+55
-36
lines changed

Package.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ import PackageDescription
1515
let package = Package(
1616
name: "swift-numerics",
1717
products: [
18-
.library(name: "Complex", targets: ["Complex"]),
18+
.library(name: "ComplexModule", targets: ["ComplexModule"]),
1919
.library(name: "Numerics", targets: ["Numerics"]),
20-
.library(name: "Real", targets: ["Real"]),
20+
.library(name: "RealModule", targets: ["RealModule"]),
2121
],
2222
dependencies: [
2323
],
2424
targets: [
25-
.target(name: "Complex", dependencies: ["Real"]),
26-
.target(name: "Numerics", dependencies: ["Complex", "Real"]),
27-
.target(name: "NumericsShims", dependencies: []),
28-
.target(name: "Real", dependencies: ["NumericsShims"]),
25+
.target(name: "ComplexModule", dependencies: ["RealModule"]),
26+
.target(name: "Numerics", dependencies: ["ComplexModule", "RealModule"]),
27+
.target(name: "_NumericsShims", dependencies: []),
28+
.target(name: "RealModule", dependencies: ["_NumericsShims"]),
2929

30-
.testTarget(name: "ComplexTests", dependencies: ["Complex", "NumericsShims"]),
31-
.testTarget(name: "RealTests", dependencies: ["Real"]),
30+
.testTarget(name: "ComplexTests", dependencies: ["ComplexModule", "_NumericsShims"]),
31+
.testTarget(name: "RealTests", dependencies: ["RealModule"]),
3232
]
3333
)

README.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ These modules fall broadly into two categories:
99

1010
There is some overlap between these two categories, and API that begins in the first category may migrate to the second as it matures and new uses are discovered.
1111

12-
Swift Numerics modules are fine-grained; if you need support for Complex numbers, you can import the Complex module without pulling in everything else in the library as well:
12+
Swift Numerics modules are fine-grained; if you need support for Complex numbers, you can import ComplexModule<sup><a name="back1">[1](#foot1)</a></sup> without pulling in everything else in the library as well:
1313
```swift
14-
import Complex
14+
import ComplexModule
1515

1616
let z = Complex<Double>.i
1717
```
@@ -51,13 +51,32 @@ To fix a bug, or make smaller improvements:
5151
Questions about how to use Swift Numerics modules, or issues that are not clearly bugs can be discussed in the ["Swift Numerics" section of the Swift forums.](https://forums.swift.org/c/related-projects/swift-numerics)
5252

5353
## Modules
54-
1. [Real](Sources/Real/README.md)
55-
2. [Complex](Sources/Complex/README.md)
54+
1. [RealModule](Sources/RealModule/README.md)
55+
2. [ComplexModule](Sources/ComplexModule/README.md)
5656

5757
## Future expansion
5858
1. [Approximate Equality](https://github.com/apple/swift-numerics/issues/3)
5959
2. [Large Fixed-Width Integers](https://github.com/apple/swift-numerics/issues/4)
6060
3. [Arbitrary-Precision Integers](https://github.com/apple/swift-numerics/issues/5)
6161
4. [Shaped Arrays](https://github.com/apple/swift-numerics/issues/6)
6262
5. [Decimal Floating-point](https://github.com/apple/swift-numerics/issues/7)
63-
6. [Float16](https://github.com/apple/swift-numerics/issues/8)
63+
64+
## Notes
65+
<sup><a name="foot1">[1](#back1)</a></sup> Swift is currently unable to use the fully-qualified name for types when a type and module have the same name (discussion here: https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482).
66+
This would prevent users of Swift Numerics who don't need generic types from doing things like:
67+
```swift
68+
import Complex
69+
// I know I only ever want Complex<Double>, so I shouldn't need the generic parameter.
70+
typealias Complex = Complex.Complex<Double> // doesn't work, because name lookup fails.
71+
```
72+
For this reason, modules that would have this ambiguity are suffixed with `Module` within Swift Numerics:
73+
```swift
74+
import ComplexModule
75+
// I know I only ever want Complex<Double>, so I shouldn't need the generic parameter.
76+
typealias Complex = ComplexModule.Complex<Double>
77+
// But I can still refer to the generic type by qualifying the name if I need it occasionally:
78+
let a = ComplexModule.Complex<Float>
79+
```
80+
The `Real` module does not contain a `Real` type, but does contain a `Real` protocol, and users may want to define their own `Real` type (and possibly re-export the `Real` module), so the suffix is also applied there.
81+
New modules have to evaluate this decision carefully, but can err on the side of adding the suffix.
82+
It's expected that most users will simply `import Numerics`, so this isn't an issue for them.

Sources/Complex/Arithmetic.swift renamed to Sources/ComplexModule/Arithmetic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
import Real
12+
import RealModule
1313

1414
// MARK: - Additive structure
1515
extension Complex: AdditiveArithmetic {

Sources/Complex/Complex.swift renamed to Sources/ComplexModule/Complex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
import Real
12+
import RealModule
1313

1414
/// A complex number represented by real and imaginary parts.
1515
///

Sources/Complex/README.md renamed to Sources/ComplexModule/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
This module provides a `Complex` number type generic over an underlying `RealType`:
44
```swift
5-
1> import Complex
5+
1> import ComplexModule
66
2> let z = Complex(1,1) // z = 1 + i
77
```
88
This module provides approximate feature parity and memory layout compatibility with C, Fortran, and C++ complex types (although the importer cannot map the types for you, buffers may be reinterpreted to shim API defined in other languages).
99

1010
The usual arithmetic operators are provided for Complex numbers, as well as conversion to and from polar coordinates and many useful properties, plus conformances to the obvious usual protocols: `Equatable`, `Hashable`, `Codable` (if the underlying `RealType` is), and `AlgebraicField` (hence also `AdditiveArithmetic` and `SignedNumeric`).
1111

1212
### Dependencies:
13-
- The `Real` module.
13+
- `RealModule`.
1414

1515
## Design notes
1616

Sources/Numerics/Numerics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
// A module that re-exports the complete Swift Numerics public API.
13-
@_exported import Real
14-
@_exported import Complex
13+
@_exported import RealModule
14+
@_exported import ComplexModule
File renamed without changes.

Sources/Real/Double+Real.swift renamed to Sources/RealModule/Double+Real.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
import NumericsShims
12+
import _NumericsShims
1313

1414
extension Double: Real {
1515
@_transparent

Sources/Real/Float+Real.swift renamed to Sources/RealModule/Float+Real.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
import NumericsShims
12+
import _NumericsShims
1313

1414
extension Float: Real {
1515
@_transparent

0 commit comments

Comments
 (0)