Skip to content

Intrinsic Procedure __assign__

IsaacShelton edited this page Mar 21, 2022 · 1 revision

'__assign__' method

The __assign__ method can be created to override the assignment = operator for struct values.

func __assign__(this *T, other T) {

}

or

func __assign__(this *T, other POD T) {

}

where T is the struct type in question.

Usage Example

import basics

struct UnorderedPair (first, second int)

func __assign__(this *UnorderedPair, other POD UnorderedPair) {
    this.first = other.second
    this.second = other.first
}

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

func main {
    pair1 UnorderedPair
    pair1.first = 1
    pair1.second = 2
    print(pair1)
    
    pair2 UnorderedPair = pair1
    print(pair2)
}
(1, 2)
(2, 1)
Clone this wiki locally