-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from dn-m/newtype
Implement NewType
- Loading branch information
Showing
4 changed files
with
172 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// | ||
// NewType.swift | ||
// DataStructures | ||
// | ||
// Created by James Bean on 8/20/18. | ||
// | ||
|
||
public protocol NewType { | ||
associatedtype Value | ||
var value: Value { get } | ||
init(value: Value) | ||
} | ||
|
||
extension NewType { | ||
public init(_ value: Value) { | ||
self.init(value: value) | ||
} | ||
} | ||
|
||
extension NewType where Value: ExpressibleByIntegerLiteral { | ||
public init(integerLiteral value: Value.IntegerLiteralType) { | ||
self.init(value: Value(integerLiteral: value)) | ||
} | ||
} | ||
|
||
extension NewType where Value: ExpressibleByFloatLiteral { | ||
public init(floatLiteral value: Value.FloatLiteralType) { | ||
self.init(value: Value(floatLiteral: value)) | ||
} | ||
} | ||
|
||
extension NewType where | ||
Value: ExpressibleByArrayLiteral, | ||
Value: RangeReplaceableCollection, | ||
Value.ArrayLiteralElement == Value.Element | ||
{ | ||
public init(arrayLiteral values: Value.ArrayLiteralElement...) { | ||
self.init(value: Value(values)) | ||
} | ||
} | ||
|
||
extension NewType where Value: Numeric { | ||
|
||
public var magnitude: Value.Magnitude { | ||
return value.magnitude | ||
} | ||
|
||
public static func + (lhs: Self, rhs: Self) -> Self { | ||
return Self(value: lhs.value + rhs.value) | ||
} | ||
|
||
public static func += (lhs: inout Self, rhs: Self) { | ||
lhs = lhs + rhs | ||
} | ||
|
||
public static func - (lhs: Self, rhs: Self) -> Self { | ||
return Self(value: lhs.value - rhs.value) | ||
} | ||
|
||
public static func -= (lhs: inout Self, rhs: Self) { | ||
lhs = lhs - rhs | ||
} | ||
|
||
public static func * (lhs: Self, rhs: Self) -> Self { | ||
return Self(value: lhs.value * rhs.value) | ||
} | ||
|
||
public static func *= (lhs: inout Self, rhs: Self) { | ||
lhs = lhs * rhs | ||
} | ||
|
||
public init?<T>(exactly source: T) where T: BinaryInteger { | ||
guard let value = Value(exactly: source) else { return nil } | ||
self.init(value: value) | ||
} | ||
} | ||
|
||
extension NewType where Value: SignedNumeric { } | ||
|
||
extension NewType where Value: Sequence { | ||
public func makeIterator() -> Value.Iterator { | ||
return value.makeIterator() | ||
} | ||
} | ||
|
||
extension NewType where Value: Collection { | ||
|
||
public typealias Iterator = Value.Iterator | ||
|
||
/// Start index. | ||
public var startIndex: Value.Index { | ||
return value.startIndex | ||
} | ||
|
||
/// End index. | ||
public var endIndex: Value.Index { | ||
return value.endIndex | ||
} | ||
|
||
/// Index after given index `i`. | ||
public func index(after i: Value.Index) -> Value.Index { | ||
return value.index(after: i) | ||
} | ||
|
||
public var indices: Value.Indices { | ||
return value.indices | ||
} | ||
|
||
/// - returns: Element at the given `index`. | ||
public subscript (index: Value.Index) -> Value.Element { | ||
return value[index] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// NewTypeTests.swift | ||
// DataStructuresTests | ||
// | ||
// Created by James Bean on 8/20/18. | ||
// | ||
|
||
import XCTest | ||
import DataStructures | ||
|
||
class NewTypeTests: XCTestCase { | ||
|
||
func testExpressibleByIntegerLiteral() { | ||
struct I: NewType, ExpressibleByIntegerLiteral { let value: Int } | ||
let _: I = 42 | ||
} | ||
|
||
func testExpressibleByFloatLiteral() { | ||
struct F: NewType, ExpressibleByFloatLiteral { let value: Double } | ||
let _: F = 42.0 | ||
} | ||
|
||
func testNumeric() { | ||
struct N: NewType, Numeric { let value: Double } | ||
let a: N = 42 | ||
let b: N = 9000 | ||
let _ = a + b | ||
let _ = a - b | ||
let _ = a * b | ||
} | ||
|
||
func testSignedNumeric() { | ||
struct SN: NewType, SignedNumeric { let value: Int } | ||
let n: SN = 1 | ||
let _ = -n | ||
} | ||
|
||
func testSequence() { | ||
struct S: NewType, Sequence { let value: [Int] } | ||
let s = S([1,2,3]) | ||
let _ = s.map { $0 } | ||
} | ||
|
||
func testCollection() { | ||
struct C: NewType, Collection { let value: [Int] } | ||
let c = C([1,2,3]) | ||
let _ = c.count | ||
} | ||
} |