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
| public class Lazy<T> { | |
| private let initializer: () -> T | |
| private var _value: T! | |
| public var value: T { | |
| get { | |
| // TODO: mutex | |
| switch _value { | |
| case .none: | |
| _value = initializer() |
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
| // goal: call `(Never instance).foo()` | |
| extension Never { | |
| func foo() { print("exec") } | |
| } | |
| unsafeBitCast((), to: Never.self).foo() // print "exec" | |
| (nil as Never?)?.foo() // ()? | |
| fatalError().foo() // runtime error |
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
| precedencegroup GroupA { | |
| associativity: none | |
| } | |
| precedencegroup GroupB { | |
| higherThan: GroupA | |
| associativity: none | |
| } |
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
| func f() { | |
| defer { | |
| print("defer") /* <- ② */ | |
| } | |
| return /* <- ③ */ print("return value") /* <- ① */ | |
| } | |
| class C { | |
| var v: () { | |
| get { } |
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
| switch "" as Optional { | |
| case _: | |
| print("_") | |
| case _?: // ⚠️ Case will never be executed | |
| print("_?") | |
| //case _??: // ❗️error: '?' pattern cannot match values of type 'String' | |
| // print("_??") | |
| default: | |
| print("s") | |
| } |
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
| switch "" as Optional { | |
| case _?: | |
| print("_?") | |
| default: | |
| print("s") | |
| } |
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
| var p: Protocol |
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
| extension Optional: CustomStringConvertible { | |
| public var description: String { | |
| switch self { | |
| case .none: | |
| return "" | |
| case .some(let wrapped): | |
| return "\(wrapped)" | |
| } | |
| } | |
| } |
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
| "\(Normal() as Optional)" | |
| // -> "Optional(description)" | |
| "\(Force() as Optional)" | |
| // -> "description" | |
| // ------------------ | |
| struct Normal: CustomStringConvertible { | |
| var description: String { return "\(#function)" } |
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
| let any: Any = 🍎() | |
| if let any = any as? 🌳 { | |
| // do something | |
| } | |
| // ----------- | |
| if case let any as 🌳 = any { | |
| // do something |