Game Development Reference
In-Depth Information
return (val1, val2)
}
var numbers = orderTwoValuesAscending(10, 7)
println("\(numbers.lesser) < \(numbers.greater)")
var characters = orderTwoValuesAscending("f", "d")
println("\(characters.lesser) < \(characters.greater)")
Let's examine the new function. You will see that the new function is similar to the earlier
function
orderTwoNumbersAscending()
. The first thing you will notice is this
function uses a
G
to represent its generic type (I chose another letter just to show that the
character used is arbitrary). This generic,
G
, is used for both parameters and values in the
returned tuple, and it is restricted to the Comparable protocol. Now, because both
String
s and
Int
s adopt the Comparable protocol, the new function can operate on
Int
s and
String
s. Go ahead and give it try. You will see the following output:
7 < 10
d < f