Skip to content

Intrinsic Procedure __equals__

IsaacShelton edited this page Mar 21, 2022 · 1 revision

'__equals__' method

The __equals__ function can be defined to allow additional types to be used with the equality == operator.

func __equals__(a $A, b $B) bool {

}

where $A and $B are any valid types

Usage Example

import basics

struct UnorderedPair (first, second int)

func __equals__(a, b UnorderedPair) bool {
    if a.first == b.first  && a.second == b.second, return true
    if a.first == b.second && a.second == b.first,  return true
    return false
}

func toString(pair UnorderedPair) String {
    return "(%, %)" % pair.first % pair.second
}

func main {
    pair1 UnorderedPair
    pair1.first = 8
    pair1.second = 13
    
    pair2 UnorderedPair
    pair2.first = 13
    pair2.second = 8
    
    pair3 UnorderedPair
    pair3.first = 7
    pair3.second = 8
    
    printComparison(pair1, pair2)
    printComparison(pair2, pair1)
    printComparison(pair1, pair3)
    printComparison(pair3, pair1)
    printComparison(pair2, pair3)
    printComparison(pair3, pair2)
}

func printComparison(a, b UnorderedPair) {
    printf("%S == %S  =>  %b\n", toString(a), toString(b), a == b)
}
(8, 13) == (13, 8)  =>  true
(13, 8) == (8, 13)  =>  true
(8, 13) == (7, 8)  =>  false
(7, 8) == (8, 13)  =>  false
(13, 8) == (7, 8)  =>  false
(7, 8) == (13, 8)  =>  false
Clone this wiki locally