Skip to content

Instantly share code, notes, and snippets.

@erica
Last active July 7, 2020 17:07
Show Gist options
  • Select an option

  • Save erica/c8e540ea2f4987d00799fde9fa0f3f96 to your computer and use it in GitHub Desktop.

Select an option

Save erica/c8e540ea2f4987d00799fde9fa0f3f96 to your computer and use it in GitHub Desktop.

Revisions

  1. erica revised this gist Jul 7, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions attempts.swift
    Original 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 {
  2. erica revised this gist Jul 7, 2020. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions attempts.swift
    Original 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 {
  3. erica revised this gist Jul 7, 2020. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion attempts.swift
    Original file line number Diff line number Diff line change
    @@ -76,8 +76,19 @@ func alsoNotDesired() -> String {
    }
    }

    // You can fallthrough into scopes with FEWER variables defined, so this order works:
    // 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"
    }
  4. erica revised this gist Jul 7, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions attempts.swift
    Original 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"

  5. erica revised this gist Jul 7, 2020. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions attempts.swift
    Original 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"
    }
    }


  6. erica revised this gist Jul 7, 2020. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions attempts.swift
    Original 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"
    }
    }

  7. erica revised this gist Jul 7, 2020. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions attempts.swift
    Original 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"
    }
    }

  8. erica created this gist Jul 7, 2020.
    29 changes: 29 additions & 0 deletions attempts.swift
    Original 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"
    }
    }