operator (~=)

Swift โŸฉ Patterns โŸฉ

โญ๏ธ ๆณจๆ„๏ผšๅœจ่‡ช่จ‚ ~= ้‹็ฎ—ๆ™‚๏ผŒไธ€ๅฎš่ฆ้ตๅพช pattern ~= value ็š„ๆจกๅผใ€‚

func ~=<P, V>(pattern: P, value: V) -> Bool

Swift uses various overloads of the ~= operator to do pattern matching, which also lets us define our own overloads ... ๐Ÿ‘‰ Sundell

Ole ๆๅŠๅฆ‚ไฝ•่จญ่จˆไธ€ๅ€‹ generic operator๏ผš

func ~=<T>(pattern: (T) -> Bool, value: T) -> Bool {
    return pattern(value)
}

Sundell ๆๅŠๅฆ‚ไฝ•่จญ่จˆไธ€ๅ€‹ generic pattern๏ผš

// Sundell ็š„ๅšๆณ•็ญ‰ๆ–ผๆ˜ฏๅฐ‡ Ole ็š„ๅšๆณ•ๅŒ…่ฃๅœจไธ€ๅ€‹ struct ่ฃก้ข๏ผŒ
// ไฝ†ๅ…ฉ่€…็š„็ฒพ็ฅžๆ˜ฏไธ€ๆจฃ็š„ใ€‚
struct Pattern<Value> {
    let match: (Value) -> Bool
}

func ~=<T>(pattern: Pattern<T>, value: T) -> Bool {
    pattern.match(value)
}

Last updated