mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 15:24:09 +03:00
Add generics operator in Swift
This commit is contained in:
parent
abd7444f9e
commit
707c8db171
@ -574,4 +574,18 @@ print(mySquare.sideLength) // 4
|
|||||||
// change side length using custom !!! operator, increases size by 3
|
// change side length using custom !!! operator, increases size by 3
|
||||||
!!!mySquare
|
!!!mySquare
|
||||||
print(mySquare.sideLength) // 12
|
print(mySquare.sideLength) // 12
|
||||||
|
|
||||||
|
// Operators can also be generics
|
||||||
|
infix operator <-> {}
|
||||||
|
func <-><T: Equatable> (inout a: T, inout b: T) {
|
||||||
|
let c = a
|
||||||
|
a = b
|
||||||
|
b = c
|
||||||
|
}
|
||||||
|
|
||||||
|
var foo: Float = 10
|
||||||
|
var bar: Float = 20
|
||||||
|
|
||||||
|
foo <-> bar
|
||||||
|
print("foo is \(foo), bar is \(bar)") // "foo is 20.0, bar is 10.0"
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user