Skip to content

Instantly share code, notes, and snippets.

Avatar

Dai Williams Daio-io

View GitHub Profile
@Daio-io
Daio-io / GuardLetKotlinExtension.kt
Created Dec 31, 2016
An example of creating a Swift like guard let extension function in Kotlin
View GuardLetKotlinExtension.kt
/**
* 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 Sep 2, 2016
Safely unwrap a WeakReference<T> without having to do chained if null check
View WeakRefUtil.java
public final class WeakRefUtil {
/**
* Safely unwrap a WeakReference<T>
*
* Returns reference if unwrapped or null
* @param reference - WeakReference<T>
* @return T
*/
@Nullable