Skip to content

Instantly share code, notes, and snippets.

View ezura's full-sized avatar

Yuka Ezura ezura

View GitHub Profile
@ezura
ezura / CodePiece.swift
Created June 24, 2017 05:29
さっきの "返り値だけジェネリクス" はこういうのかな(´・ω・`)? その前のスライドで考えてたら聞きそびれた… #swift #CodePiece #ジェネリクス勉強会
func f<T: ExpressibleByIntegerLiteral>() -> T {
return 1 // as T
}
let int: Int = f()
let double: Double = f()
@ezura
ezura / inout.kt
Last active June 24, 2017 04:44
p.163
class B<out Any> {}
val b: B<String> = B()
val b_: B<out Any> = b // out あり
b_
// B@5327a06e
val b: B<String> = B()
val b_: B<Any> = b // out なし
b_
@ezura
ezura / CodePiece.kt
Created June 20, 2017 12:48
extensnion で Constractor 作れなかったけど、呼び出し側の見た目だけならできた ( 'ω') #CodePiece #kotlin
class A {}
fun A(v: Int): A = A()
/*
A()
A(1)
*/
@ezura
ezura / constractor.kt
Created June 20, 2017 12:43
呼び出し側の見た目だけ Constractor の拡張っぽいものできた
class A {}
fun A(v: Int): A = A()
/*
A()
A(1)
*/
@ezura
ezura / CodePiece.swift
Created June 20, 2017 12:08
while でキャプチャリストは毎回評価されるのか試してみた。評価されてた #swift #CodePiece
var i = 0
repeat { [_ = print("capture list")]
i += 1
} while i < 3
/*
capture list
capture list
capture list
*/
var v = true
repeat { [v]
print(v)
v = false
print(v)
} while v
var i = 0
repeat { [_ = print("capture list")]
i += 1
class A : Iterable<String> {
override operator fun iterator(): Iterator<String> = MyIterator()
}
class MyIterator: Iterator<String> {
override operator fun hasNext(): Boolean = Math.random() < 0.5
override operator fun next(): String = "a"
}
// A().forEach({ print(it)})
@ezura
ezura / Set.kt
Created June 19, 2017 12:54
Kotlin の Set の要素の比較、Element に制約をつけてるようにも見えないし、どうしてるんだろうと思ったら、Any に `fun equals(other: Any?): Boolean` いるからどの型でも等しいか検査可能なのね(。 ・ω・))
class A(val v1: Int) {
init {
require(v1 < 0, {"aaaaaa"})
}
override fun hashCode(): Int {
return v1
}
override fun equals(other: Any?): Boolean {
@ezura
ezura / CodePiece.kt
Created June 19, 2017 12:38
Set(List とかでも) のインスタンスを作るとき、要素数によって、作られてる実態が違うみたい…? #CodePiece #kotlin
// elements.size == 0
object EmptySet : Set<Nothing>
// elements.size == 1
java.util.Collections.singleton(element)
// elements.size > 1
public typealias LinkedHashSet<E> = java.util.LinkedHashSet<E>
@ezura
ezura / CodePiece.swift
Created June 19, 2017 06:31
( ・ω・)) #swift #CodePiece #kotlin
// Swift
func TODO() -> Never { fatalError() }
func start() -> String { return TODO() } // 返り値の型が Never でもコンパイル通る
// Kotlin
fun TODO(): Nothing = throw NotImplementedError()
fun start(): String = TODO()