Skip to content

Instantly share code, notes, and snippets.

View ezura's full-sized avatar

Yuka Ezura ezura

View GitHub Profile
@ezura
ezura / CodePiece.kt
Created July 6, 2017 02:33
む…。畳み込み?だとしても、300 === 300 だし参照は等価じゃないような… 127 まではプールされてるの使いまわされるのはわかるんだけど… #CodePiece #kotlin
val v: Int? = 300
val v2: Int? = 300
v === v2
// false
val v: Int = 300
val v2: Int = 300
v === v2
// true
@ezura
ezura / CodePiece.swift
Created July 5, 2017 11:17
値を unwrap しつつ、できなかったら例外投げるやつ。 上と下、同じことが実現できるけど、印象違うなぁ(。 ・ω・)) #swift #CodePiece
// 1
guard let v = v else {
throw NSError()
}
// 2
let v = try v ?? { throw NSError() }()
@ezura
ezura / accessControl1.kt
Last active July 18, 2017 05:39
kotlin 可視性実験
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()
@ezura
ezura / gist:90f7186027cbe2c1822c5d2f85d72061
Created July 1, 2017 06:45
kotlin は T に対して拡張できる。T.() できる。
// 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#>)
@ezura
ezura / CodePiece.swift
Last active September 8, 2017 03:19
Swift で Kotlin を真似た lazy 作ってみた上が作ったもの (型で実装。Swift に delegation ないので無理やりな部分あり)下が Swift 標準 (lazy は予約語) #swift #CodePiece #kotlin
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()
@ezura
ezura / lazy.swift
Created July 1, 2017 01:28
kotlin の lazy を真似て実装
import Foundation
// I could not nest in `Lazy<T>` :(
// to handle Optional
private enum Value<T> {
case uninitialized
case initialized(T)
}
class Lazy<T> {
@ezura
ezura / object.kt
Last active June 29, 2017 04:59
返り値の型は P
interface P {}
class A {
val obj = object: P {
}
}
val a: P = A().obj
@ezura
ezura / CodePiece.swift
Created June 27, 2017 00:55
Swift も三項演算子内で例外投げられるよって夢で話したので、ちゃんと証明しておこう( ˘ω˘) #swift #CodePiece
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() }()
}
@ezura
ezura / intout.swift
Created June 26, 2017 04:21
kotlin での optional のやつ見て思ったこと。エラーメッセージしっかりしててくれた。「inout つけた場合に"書き換え(in)"も許可する必要があるので不変じゃないといけなくなる」という認識であってるかな…
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)
@ezura
ezura / CodePiece.swift
Created June 25, 2017 02:46
Swift4 で associatedType に再帰と条件付けできるようになったらしいから試してみたんだけど、コンパイル通るけど解釈してくれない…(´・ω・`) #swift #CodePiece
protocol P {
associatedtype T
associatedtype U: Self where U.T == T
}