Skip to content

Instantly share code, notes, and snippets.

View Daio-io's full-sized avatar

Dai Williams Daio-io

View GitHub Profile
@Daio-io
Daio-io / GuardLetKotlinExtension.kt
Created December 31, 2016 15:07
An example of creating a Swift like guard let extension function in Kotlin
/**
* Guard let in Kotlin
**/
inline infix fun <reified T> T?.guard(call: () -> Unit): T? {
if (this is T) return this
else {
call()
return null
}
}
@Daio-io
Daio-io / WeakRefUtil.java
Created September 2, 2016 18:01
Safely unwrap a WeakReference<T> without having to do chained if null check
public final class WeakRefUtil {
/**
* Safely unwrap a WeakReference<T>
*
* Returns reference if unwrapped or null
* @param reference - WeakReference<T>
* @return T
*/
@Nullable