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() { | |
| operator fun plus(that: Int): Int { | |
| return that + 1 | |
| } | |
| } | |
| A() + 1 | |
| // error | |
| // 1 + 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
| let strings: Array<String> = ["", ""] | |
| var anys: Array<Any> = strings | |
| anys[0] = 1 |
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 PermissionMode: Permission = Default | |
| func f() -> String // これを差し替える | |
| } | |
| extension P { /* 略 */ } | |
| extension P where Self.PermissionMode == Strict { /* 略 */ } | |
| struct A: P {} | |
| struct B: P { | |
| typealias PermissionMode = Strict |
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 Permission {} | |
| enum Default: Permission {} | |
| enum Strict: Permission {} | |
| protocol P { | |
| associatedtype PermissionMode: Permission = Default | |
| func f() -> String | |
| } | |
| extension P { |
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
| {} /* () -> Void */ is Any |
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
| // Kotlin | |
| val itr = (1..2).iterator() | |
| println(itr.next()) // 1 | |
| println(itr.next()) // 2 | |
| // !!!: Exception in thread "main" java.util.NoSuchElementException | |
| println(itr.next()) | |
| // Swift | |
| var itr = [1, 2].makeIterator() | |
| itr.next() // 1 |
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
| // # Iterator の実装 | |
| // Kotlin | |
| // 次の要素があるかどうかは hasNext みる | |
| fun next(): T | |
| fun hasNext(): Boolean | |
| // Swift | |
| // 次の要素があるかは next() の返り値の型が Self.Element? なので、これだけで判断可能 | |
| func next() -> Self.Element? |
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 Type: NSObject { | |
| let value = 1 | |
| } | |
| let staticString: StaticString = #keyPath(Type.value) | |
| let string: String = #keyPath(Type.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
| else { | |
| if cond2 { | |
| } | |
| } | |
| // else の {} を省略して | |
| else if cond2 { | |
| } |
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 Obj { | |
| func f() { /* do something */ } | |
| } | |
| // obj そのあと使わないのにそのスコープに存在し続けるのが気持ち悪い (超個人的な主観) | |
| // (do {} 使うのも手だけど…) | |
| let obj: Obj = { | |
| if true /* condition */ { | |
| return Obj() | |
| } else { |