Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func whatever(foo: @autoclosure () -> Int) {
- let x = foo()
- print(x)
- }
- whatever(foo: 5)
- // prints: 5
- whatever(foo: { 5 })
- // Error: Function produces expected type 'Int'; did you mean to call it with '()'?
- struct Defaults {
- static var dispatcher: Defaults = ...
- subscript<T>(setting: Setting<T>) -> T { ... }
- struct Setting<T> {
- let key: String
- let defaultValue: () -> T
- init(key: String, defaultValue: @escaping @autoclosure () -> T) {
- self.key = key
- self.defaultValue = defaultValue
- }
- }
- }
- extension Defaults.Setting {
- static var nickname: Defaults.Setting<String> {
- return Defaults.Setting(key: "__nickname", defaultValue: "Angela Merkel")
- }
- }
- // Usage:
- Defaults.dispatcher[.nickname] = "Emmanuel Macron"
- extension Defaults.Setting {
- var withHashedKey: Defaults.Setting<T> {
- return Defaults.Setting(key: key.md5(), defaultValue: defaultValue)
- // Error: Cannot convert return expression of type 'Defaults.Setting<() -> T>' to return type 'Defaults.Setting<T>'
- }
- }
- extension Defaults.Setting {
- init(key: String, defaultValue: @escaping () -> T) {
- self.init(key: key, defaultValue: defaultValue)
- }
- }
Add Comment
Please, Sign In to add comment