Created
August 14, 2020 09:00
-
-
Save cweinberger/fd395b12a0c75834e1ba55b17838062e to your computer and use it in GitHub Desktop.
Let default value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
+ less boilerplate | |
+ default initializer | |
- you can alter `limit` | |
*/ | |
struct MyRequestInput1 { | |
let searchTerm: String | |
var limit: Int = 10 | |
} | |
_ = MyRequestInput1(searchTerm: "foo") | |
_ = MyRequestInput1(searchTerm: "foo", limit: 20) | |
/** | |
+ you can't alter `limit` | |
+ default initializer | |
- boilerplate | |
*/ | |
struct MyRequestInput2 { | |
let searchTerm: String | |
let limit: Int | |
init(searchTerm: String, limit: Int = 10) { | |
self.searchTerm = searchTerm | |
self.limit = limit | |
} | |
} | |
_ = MyRequestInput2(searchTerm: "foo") | |
_ = MyRequestInput2(searchTerm: "foo", limit: 20) | |
/** | |
+ you can't alter `limit` outside of the file | |
+ default initializer | |
+ less boilerplate | |
- potentially less clear than `let` | |
*/ | |
struct MyRequestInput3 { | |
let searchTerm: String | |
private(set) var limit: Int = 10 | |
} | |
_ = MyRequestInput3(searchTerm: "foo") | |
_ = MyRequestInput3(searchTerm: "foo", limit: 20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment