Last active
July 7, 2020 17:07
-
-
Save erica/c8e540ea2f4987d00799fde9fa0f3f96 to your computer and use it in GitHub Desktop.
Revisions
-
erica revised this gist
Jul 7, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ Olivier: I think that's one of the rare cases where fallthough is acceptable import XCTest class Tester: XCTestCase { -
erica revised this gist
Jul 7, 2020 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,16 @@ import XCTest class Tester: XCTestCase { func testit() { XCTAssertEqual("a", switchTheFallthroughOrder(foo: .a)) XCTAssertEqual("b", switchTheFallthroughOrder(foo: .b("x"))) XCTAssertEqual("c", switchTheFallthroughOrder(foo: .b("cx"))) XCTAssertEqual("c", switchTheFallthroughOrder(foo: .c)) } } Tester.defaultTestSuite.run() // Goal func Goal() -> String { switch foo { -
erica revised this gist
Jul 7, 2020 . 1 changed file with 12 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -76,8 +76,19 @@ func alsoNotDesired() -> String { } } // Greg Titus: You can fallthrough into scopes with FEWER variables defined, so this order works: case .b(let str) where str.hasPrefix("c"): fallthrough case .c: return "c" // Suyash Srijan 10:52 AM // I think the best you can do is something like this: switch foo { case .a: return "a" case .b: guard case let .b(str) = foo, str.hasPrefix("c") else { return "b" } fallthrough case .c: return "c" } -
erica revised this gist
Jul 7, 2020 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -76,4 +76,8 @@ func alsoNotDesired() -> String { } } // You can fallthrough into scopes with FEWER variables defined, so this order works: case .b(let str) where str.hasPrefix("c"): fallthrough case .c: return "c" -
erica revised this gist
Jul 7, 2020 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -67,3 +67,13 @@ func test3() -> String { } } // Also a non-winner although it works: func alsoNotDesired() -> String { switch foo { case .a: return "a" case .b(let str) where !str.hasPrefix("c"): return "b" default: return "c" } } -
erica revised this gist
Jul 7, 2020 . 1 changed file with 28 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -39,3 +39,31 @@ func test2() -> String { } } /* error: macOS.playground:23:26: error: expected expression for 'where' guard of 'case' case .c(let _) where let str = "c": fallthrough ^ error: macOS.playground:23:26: error: expected ':' after 'case' case .c(let _) where let str = "c": fallthrough ^ error: macOS.playground:23:39: error: consecutive statements on a line must be separated by ';' case .c(let _) where let str = "c": fallthrough ^ ; error: macOS.playground:23:39: error: expected expression case .c(let _) where let str = "c": fallthrough ^ */ func test3() -> String { switch foo { case .a: return "a" case .c(let _) where let str = "c": fallthrough case.b(let str) where str.hasPrefix("c"): return "c" case .b: return "b" } } -
erica revised this gist
Jul 7, 2020 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,3 +27,15 @@ func obviousWayButNotDesired() -> String { case .b: return "b" } } // By default, the ~= operator compares two values of the same type using the == operator. func test2() -> String { switch foo { case .a: return "a" // Error: Operator function ~= requires that Foo conform to Equatable case _ where foo ~= .c : return "c" case .b(let str) where str.hasPrefix("c"): return "c" case .b: return "b" } } -
erica created this gist
Jul 7, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ // Goal func Goal() -> String { switch foo { case .a: return "a" // Error: str must be bound in every pattern case .b(let str) where str.hasPrefix("c"), .c: return "c" case .b: return "b" } } // Fallthrough doesn't work func test1() -> String { switch foo { case .a: return "a" // Error: fallthrough from a case which doesn't bind variable 'str' case .c: fallthrough case .b(let str) where str.hasPrefix("c"): return "c" case .b: return "b" } } func obviousWayButNotDesired() -> String { switch foo { case .a: return "a" case .c: return "c" case .b(let str) where str.hasPrefix("c"): return "c" case .b: return "b" } }