Skip to content

Instantly share code, notes, and snippets.

@cweinberger
Created August 14, 2020 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cweinberger/fd395b12a0c75834e1ba55b17838062e to your computer and use it in GitHub Desktop.
Save cweinberger/fd395b12a0c75834e1ba55b17838062e to your computer and use it in GitHub Desktop.
Let default value
/**
+ 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