Skip to content

~= is inappropriate #1

@lilyball

Description

@lilyball

This is not an appropriate use of ~=. The ~= operator in Swift is called "pattern match", but what it's specifically for is matching values in case statements. The ~= operator is how you can use nil and a..<b intervals in case statements, as well as Equatable values. The code

switch foo {
case 1..<10:
    bar()
default:
    baz()
}

is equivalent to

switch foo {
case temp where 1..<10 ~= temp:
    bar()
default:
    baz()

(except without binding any identifiers).

Repurposing it here as a sort of pattern-match assignment is at the minimum very confusing. You should be using a different custom operator instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions