Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3568] Cannot write custom pattern matching operator with enum pattern type #46156

Open
swift-ci opened this issue Jan 6, 2017 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jan 6, 2017

Previous ID SR-3568
Radar None
Original Reporter ianterrell (JIRA User)
Type Bug
Environment

macOS Sierra Version 10.12.2 (16C67)
Xcode Version 8.2 (8C38)

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: a4f994ae56f17272258c1e9ae6d2181a

relates to:

  • SR-1121 Custom pattern matching with enum types in Swift 2.2 not working as expected

Issue Description:

Going from this working code in the Swift Language Guide:

func ~=(pattern: String, value: Int) -> Bool {
    return pattern == "\(value)"
}

let point = (0, 0)
switch point {
case ("0", "0"):
    print("(0, 0) is at the origin.")
default:
    print("The point is at (\(point.0), \(point.1)).")
}

I expect to be able to write the following pattern matcher:

enum Pattern {
    case zero
}

func ~=(pattern: Pattern, value: Int) -> Bool {
    return value == 0 && pattern == .zero
}

switch point {
case (Pattern.zero, Pattern.zero):
    print("(0, 0) is at the origin.")
default:
    print("The point is at (\(point.0), \(point.1)).")
}

However, I observe that the code does not compile, with the following errors:

error: CustomPatternMatchEnum.playground:20:15: error: enum case 'zero' is not a member of type 'Int'
case (Pattern.zero, Pattern.zero):
              ^

error: CustomPatternMatchEnum.playground:20:29: error: enum case 'zero' is not a member of type '<<error type>>'
case (Pattern.zero, Pattern.zero):
                            ^

I can however work around the issue by using another data type. The following struct based code compiles and runs as expected:

struct Pattern {
    let sentinel: Int
    static let zero = Pattern(sentinel: 0)
}

extension Pattern: Equatable {
    static func ==(lhs: Pattern, rhs: Pattern) -> Bool {
        return lhs.sentinel == rhs.sentinel
    }
}

func ~=(pattern: Pattern, value: Int) -> Bool {
    return value == 0 && pattern == .zero
}

switch point {
case (Pattern.zero, Pattern.zero):
    print("(0, 0) is at the origin.")
default:
    print("The point is at (\(point.0), \(point.1)).")
}
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant