├── Tests ├── LinuxMain.swift └── IntersectionTests │ ├── XCTestManifests.swift │ ├── DecodableTests.swift │ └── IntersectionTests.swift ├── Sources └── Intersection │ ├── Intersection+Decodable.swift │ └── Intersection.swift ├── README.md ├── LICENSE ├── Package.swift └── .gitignore /Tests/LinuxMain.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | import IntersectionTests 4 | 5 | var tests = [XCTestCaseEntry]() 6 | tests += IntersectionTests.allTests() 7 | XCTMain(tests) 8 | -------------------------------------------------------------------------------- /Tests/IntersectionTests/XCTestManifests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | #if !canImport(ObjectiveC) 4 | public func allTests() -> [XCTestCaseEntry] { 5 | return [ 6 | testCase(IntersectionTests.allTests), 7 | ] 8 | } 9 | #endif 10 | -------------------------------------------------------------------------------- /Sources/Intersection/Intersection+Decodable.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | extension Intersection: Decodable where Value1: Decodable, Value2: Decodable { 4 | public init(from decoder: Decoder) throws { 5 | let container = try decoder.singleValueContainer() 6 | self.value1 = try container.decode(Value1.self) 7 | self.value2 = try container.decode(Value2.self) 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Swift-Intersection 2 | Extensible records / intersection type in Swift. 3 | 4 | ```swift 5 | struct S1 { 6 | var value1: Int 7 | } 8 | 9 | struct S2 { 10 | var value2: Bool 11 | } 12 | 13 | let intersection = Intersection(S1(1), S2(true)) 14 | 15 | // Type of `intersection` works similar to `S1 & S2` (intersection type) in TypeScript), i.e.: 16 | // 17 | // struct S1_And_S2 { 18 | // var value1: Int 19 | // var value2: Bool 20 | // } 21 | 22 | XCTAssertEqual(intersection.value1, 1) 23 | XCTAssertEqual(intersection.value2, true) 24 | ``` 25 | 26 | See also https://twitter.com/inamiy/status/1368468757702569986 . 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Yasuhiro Inami 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.3 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "Intersection", 8 | products: [ 9 | // Products define the executables and libraries a package produces, and make them visible to other packages. 10 | .library( 11 | name: "Intersection", 12 | targets: ["Intersection"]), 13 | ], 14 | dependencies: [ 15 | // Dependencies declare other packages that this package depends on. 16 | // .package(url: /* package url */, from: "1.0.0"), 17 | ], 18 | targets: [ 19 | // Targets are the basic building blocks of a package. A target can define a module or a test suite. 20 | // Targets can depend on other targets in this package, and on products in packages this package depends on. 21 | .target( 22 | name: "Intersection", 23 | dependencies: []), 24 | .testTarget( 25 | name: "IntersectionTests", 26 | dependencies: ["Intersection"]), 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /Tests/IntersectionTests/DecodableTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import Intersection 3 | 4 | final class DecodableTests: XCTestCase { 5 | func testDecodable() throws { 6 | let json = """ 7 | { 8 | "value1": 1, 9 | "value2": true 10 | } 11 | """ 12 | let intersection = try JSONDecoder().decode(Intersection.self, from: json.data(using: .utf8)!) 13 | XCTAssertEqual(intersection.value1, 1) 14 | XCTAssertEqual(intersection.value2, true) 15 | } 16 | 17 | func testDecodable2() throws { 18 | let json = """ 19 | { 20 | "value1": 1, 21 | "value2": true, 22 | "value3": { 23 | "value1": 2 24 | } 25 | } 26 | """ 27 | let intersection = try JSONDecoder().decode(Intersection3.self, from: json.data(using: .utf8)!) 28 | XCTAssertEqual(intersection.value1, 1) 29 | XCTAssertEqual(intersection.value2, true) 30 | XCTAssertEqual(intersection.value3, S1(2)) 31 | } 32 | 33 | static var allTests = [ 34 | ("testDecodable", testDecodable), 35 | ("testDecodable2", testDecodable2) 36 | ] 37 | } 38 | 39 | // MARK: - Fixtures 40 | 41 | private struct S1: Decodable, Equatable { 42 | var value1: Int 43 | init(_ value: Int) { self.value1 = value } 44 | } 45 | 46 | private struct S2: Decodable, Equatable { 47 | var value2: Bool 48 | init(_ value: Bool) { self.value2 = value } 49 | } 50 | 51 | private struct S3: Decodable, Equatable { 52 | var value3: S1 53 | init(_ value: S1) { self.value3 = value } 54 | } 55 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /Sources/Intersection/Intersection.swift: -------------------------------------------------------------------------------- 1 | public protocol IntersectionProtocol { 2 | associatedtype Value1 3 | associatedtype Value2 4 | 5 | init(_ value1: Value1, _ value2: Value2) 6 | } 7 | 8 | @dynamicMemberLookup 9 | public struct Intersection: IntersectionProtocol { 10 | var value1: Value1 11 | var value2: Value2 12 | 13 | public init(_ value1: Value1, _ value2: Value2) { 14 | self.value1 = value1 15 | self.value2 = value2 16 | } 17 | 18 | public subscript(dynamicMember keyPath: WritableKeyPath) -> T { 19 | get { value1[keyPath: keyPath] } 20 | set { value1[keyPath: keyPath] = newValue } 21 | } 22 | 23 | public subscript(dynamicMember keyPath: WritableKeyPath) -> T { 24 | get { value2[keyPath: keyPath] } 25 | set { value2[keyPath: keyPath] = newValue } 26 | } 27 | } 28 | 29 | extension Intersection where Value2: IntersectionProtocol { 30 | public init( 31 | _ value1: Value1, 32 | _ value21: Value2.Value1, 33 | _ value22: Value2.Value2 34 | ) { 35 | self.value1 = value1 36 | self.value2 = .init(value21, value22) 37 | } 38 | } 39 | 40 | extension Intersection where Value2: IntersectionProtocol, Value2.Value2: IntersectionProtocol { 41 | public init( 42 | _ value1: Value1, 43 | _ value21: Value2.Value1, 44 | _ value221: Value2.Value2.Value1, 45 | _ value222: Value2.Value2.Value2 46 | ) { 47 | self.value1 = value1 48 | self.value2 = .init(value21, .init(value221, value222)) 49 | } 50 | } 51 | 52 | extension Intersection where Value2: IntersectionProtocol, Value2.Value2: IntersectionProtocol, 53 | Value2.Value2.Value2: IntersectionProtocol { 54 | public init( 55 | _ value1: Value1, 56 | _ value21: Value2.Value1, 57 | _ value221: Value2.Value2.Value1, 58 | _ value2221: Value2.Value2.Value2.Value1, 59 | _ value2222: Value2.Value2.Value2.Value2 60 | ) { 61 | self.value1 = value1 62 | self.value2 = .init(value21, .init(value221, .init(value2221, value2222))) 63 | } 64 | } 65 | 66 | public typealias Intersection3 = Intersection> 67 | public typealias Intersection4 = Intersection3> 68 | public typealias Intersection5 = Intersection4> 69 | 70 | // MARK: - Equatable / Hashable 71 | 72 | extension Intersection: Equatable where Value1: Equatable, Value2: Equatable {} 73 | extension Intersection: Hashable where Value1: Hashable, Value2: Hashable {} 74 | -------------------------------------------------------------------------------- /Tests/IntersectionTests/IntersectionTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | import Intersection 3 | 4 | final class IntersectionTests: XCTestCase { 5 | func testIntersection() { 6 | let intersection = Intersection(S1(1), S2(true)) 7 | let intersection2 = Intersection(S1(1), S2(true)) 8 | 9 | XCTAssertEqual(intersection.value1, 1) 10 | XCTAssertEqual(intersection.value2, true) 11 | 12 | // Hashable compile test 13 | XCTAssertEqual(intersection, intersection2) 14 | XCTAssertEqual(intersection.hashValue, intersection2.hashValue) 15 | } 16 | 17 | func testIntersection3() { 18 | let intersection = Intersection3(S1(1), S2(true), S3("")) 19 | let intersection2 = Intersection3(S1(1), S2(true), S3("")) 20 | 21 | XCTAssertEqual(intersection.value1, 1) 22 | XCTAssertEqual(intersection.value2, true) 23 | XCTAssertEqual(intersection.value3, "") 24 | 25 | // Hashable compile test 26 | XCTAssertEqual(intersection, intersection2) 27 | XCTAssertEqual(intersection.hashValue, intersection2.hashValue) 28 | } 29 | 30 | func testIntersection4() { 31 | let intersection = Intersection4(S1(1), S2(true), S3(""), S4(0.1)) 32 | let intersection2 = Intersection4(S1(1), S2(true), S3(""), S4(0.1)) 33 | 34 | XCTAssertEqual(intersection.value1, 1) 35 | XCTAssertEqual(intersection.value2, true) 36 | XCTAssertEqual(intersection.value3, "") 37 | XCTAssertEqual(intersection.value4, 0.1) 38 | 39 | // Hashable compile test 40 | XCTAssertEqual(intersection, intersection2) 41 | XCTAssertEqual(intersection.hashValue, intersection2.hashValue) 42 | } 43 | 44 | func testIntersection5() { 45 | let intersection = Intersection5(S1(1), S2(true), S3(""), S4(0.1), S5(0)) 46 | let intersection2 = Intersection5(S1(1), S2(true), S3(""), S4(0.1), S5(0)) 47 | 48 | XCTAssertEqual(intersection.value1, 1) 49 | XCTAssertEqual(intersection.value2, true) 50 | XCTAssertEqual(intersection.value3, "") 51 | XCTAssertEqual(intersection.value4, 0.1) 52 | XCTAssertEqual(intersection.value5, 0) 53 | 54 | // Hashable compile test 55 | XCTAssertEqual(intersection, intersection2) 56 | XCTAssertEqual(intersection.hashValue, intersection2.hashValue) 57 | } 58 | 59 | static var allTests = [ 60 | ("testIntersection", testIntersection), 61 | ("testIntersection3", testIntersection3), 62 | ("testIntersection4", testIntersection4), 63 | ("testIntersection5", testIntersection5), 64 | ] 65 | } 66 | 67 | // MARK: - Fixtures 68 | 69 | private struct S1: Hashable { 70 | var value1: Int 71 | init(_ value: Int) { self.value1 = value } 72 | } 73 | 74 | private struct S2: Hashable { 75 | var value2: Bool 76 | init(_ value: Bool) { self.value2 = value } 77 | } 78 | 79 | private struct S3: Hashable { 80 | var value3: String 81 | init(_ value: String) { self.value3 = value } 82 | } 83 | 84 | private struct S4: Hashable { 85 | var value4: Float 86 | init(_ value: Float) { self.value4 = value } 87 | } 88 | 89 | private struct S5: Hashable { 90 | var value5: UInt8 91 | init(_ value: UInt8) { self.value5 = value } 92 | } 93 | --------------------------------------------------------------------------------