Created
January 28, 2016 15:26
-
-
Save lucidfrontier45/8d3dddc0d8a0d232e997 to your computer and use it in GitHub Desktop.
make kotlin's nullables act like scala's option
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
| package com.frontier45 | |
| /** | |
| * Created by du on 16/01/29. | |
| */ | |
| fun <T1, T2> T1?.map(f: (T1) -> T2): T2? { | |
| return if (this != null) { | |
| f(this) | |
| } else { | |
| null | |
| } | |
| } | |
| fun <T> T?.forEach(f: (T) -> Unit): Unit { | |
| if (this != null) { | |
| f(this) | |
| } | |
| } | |
| fun <T> T?.getOrElse(f: () -> T): T { | |
| return this ?: f() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment