- update examples

- add examples for labeled tuples and computed properties
This commit is contained in:
C. Bess 2015-11-09 17:54:05 -06:00
parent 3a7e00127f
commit afc5ea1465

View File

@ -211,7 +211,7 @@ greet("Bob", "Tuesday")
func greet2(#requiredName: String, externalParamName localParamName: String) -> String { func greet2(#requiredName: String, externalParamName localParamName: String) -> String {
return "Hello \(requiredName), the day is \(localParamName)" return "Hello \(requiredName), the day is \(localParamName)"
} }
greet2(requiredName:"John", externalParamName: "Sunday") greet2(requiredName: "John", externalParamName: "Sunday")
// Function that returns multiple items in a tuple // Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) { func getGasPrices() -> (Double, Double, Double) {
@ -224,6 +224,16 @@ let (_, price1, _) = pricesTuple // price1 == 3.69
println(price1 == pricesTuple.1) // true println(price1 == pricesTuple.1) // true
println("Gas price: \(price)") println("Gas price: \(price)")
// Named tuple params
func getGasPrices2() -> (lowestPrice: Double, highestPrice: Double, midPrice: Double) {
return (1.77, 37.70, 7.37)
}
let pricesTuple2 = getGasPrices2()
let price2 = pricesTuple2.lowestPrice
let (_, price3, _) = pricesTuple2
println(pricesTuple2.highestPrice == pricesTuple2.1) // true
println("Highest gas price: \(pricesTuple2.highestPrice)")
// Variadic Args // Variadic Args
func setup(numbers: Int...) { func setup(numbers: Int...) {
// its an array // its an array
@ -337,6 +347,11 @@ internal class Rect: Shape {
} }
} }
// Computed properties must be declared as `var`, you know, cause they can change
var smallestSideLength: Int {
return self.sideLength - 1
}
// Lazily load a property // Lazily load a property
// subShape remains nil (uninitialized) until getter called // subShape remains nil (uninitialized) until getter called
lazy var subShape = Rect(sideLength: 4) lazy var subShape = Rect(sideLength: 4)