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
| val v: Int? = 300 | |
| val v2: Int? = 300 | |
| v === v2 | |
| // false | |
| val v: Int = 300 | |
| val v2: Int = 300 | |
| v === v2 | |
| // true |
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
| // 1 | |
| guard let v = v else { | |
| throw NSError() | |
| } | |
| // 2 | |
| let v = try v ?? { throw NSError() }() |
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
| class A { | |
| private fun f() = print("private") | |
| fun _f() = f() // use `private fun f()` | |
| } | |
| fun A.f() = print("extension") | |
| fun A._f() = print("extension _f") // ignored 😖 | |
| /* | |
| val a = A() |
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 inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this } | |
| // Swift だとできない | |
| func apply<T>(_ sender: T, block: (T) -> Void) -> T { | |
| block(sender) | |
| return sender | |
| } | |
| apply(<#T##sender: T##T#>, block: <#T##(T) -> Void#>) |
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
| struct A { | |
| init() { print("call `A.init`") } | |
| } | |
| class B { | |
| var v1 = lazy { return A() } // ❗️self making | |
| lazy var v2 = { return A() }() // swift's `lazy` | |
| } | |
| let b = B() | |
| b.v1[] // A() |
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
| import Foundation | |
| // I could not nest in `Lazy<T>` :( | |
| // to handle Optional | |
| private enum Value<T> { | |
| case uninitialized | |
| case initialized(T) | |
| } | |
| class Lazy<T> { |
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
| interface P {} | |
| class A { | |
| val obj = object: P { | |
| } | |
| } | |
| val a: P = A().obj |
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 g(_ v: Bool) throws { | |
| v ? ()/* something */ : try { throw NSError() }() | |
| } | |
| protocol P { init() } | |
| func g_<T: P>(_ v: Bool) throws -> T { | |
| return v ? T() : try { throw NSError() }() | |
| } |
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(_ v: Any?) {} | |
| func f_inout(_ v: inout Any?) { /* v = nil */ } | |
| var v: Any = 1 | |
| // OK | |
| f(v) | |
| // ❗️error: cannot pass immutable value as inout argument: implicit conversion from 'Any' to 'Any?' requires a temporary | |
| //f_inout(&v) |
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
| protocol P { | |
| associatedtype T | |
| associatedtype U: Self where U.T == T | |
| } |