Skip to content

Commit

Permalink
Swift 4.2 Compatibility (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jun 5, 2019
1 parent 012f5f8 commit 926e8e0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:4.2
import Foundation
import PackageDescription

Expand All @@ -18,6 +18,10 @@ var package = Package(

.target(name: "TaggedTime", dependencies: ["Tagged"]),
.testTarget(name: "TaggedTimeTests", dependencies: ["TaggedTime"]),
],
swiftLanguageVersions: [
.version("5"),
.v4_2,
]
)

Expand Down
38 changes: 38 additions & 0 deletions Sources/Tagged/Tagged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ extension Tagged: LosslessStringConvertible where RawValue: LosslessStringConver
}
}

#if compiler(>=5)
extension Tagged: AdditiveArithmetic where RawValue: AdditiveArithmetic {
public static var zero: Tagged {
return self.init(rawValue: .zero)
Expand Down Expand Up @@ -154,6 +155,43 @@ extension Tagged: Numeric where RawValue: Numeric {
lhs.rawValue *= rhs.rawValue
}
}
#else
extension Tagged: Numeric where RawValue: Numeric {
public typealias Magnitude = RawValue.Magnitude

public init?<T>(exactly source: T) where T: BinaryInteger {
guard let rawValue = RawValue(exactly: source) else { return nil }
self.init(rawValue: rawValue)
}
public var magnitude: RawValue.Magnitude {
return self.rawValue.magnitude
}

public static func + (lhs: Tagged<Tag, RawValue>, rhs: Tagged<Tag, RawValue>) -> Tagged<Tag, RawValue> {
return self.init(rawValue: lhs.rawValue + rhs.rawValue)
}

public static func += (lhs: inout Tagged<Tag, RawValue>, rhs: Tagged<Tag, RawValue>) {
lhs.rawValue += rhs.rawValue
}

public static func * (lhs: Tagged, rhs: Tagged) -> Tagged {
return self.init(rawValue: lhs.rawValue * rhs.rawValue)
}

public static func *= (lhs: inout Tagged, rhs: Tagged) {
lhs.rawValue *= rhs.rawValue
}

public static func - (lhs: Tagged, rhs: Tagged) -> Tagged<Tag, RawValue> {
return self.init(rawValue: lhs.rawValue - rhs.rawValue)
}

public static func -= (lhs: inout Tagged<Tag, RawValue>, rhs: Tagged<Tag, RawValue>) {
lhs.rawValue -= rhs.rawValue
}
}
#endif

extension Tagged: Hashable where RawValue: Hashable {
public func hash(into hasher: inout Hasher) {
Expand Down

0 comments on commit 926e8e0

Please sign in to comment.