added a helper method for easily converting from a nullable type to a non nullable type with a non-lazy value, or lazy factory function

This commit is contained in:
James Strachan
2012-02-06 16:48:44 +00:00
parent 5f8a71eb21
commit 5057bf732e
2 changed files with 60 additions and 1 deletions
+21 -1
View File
@@ -68,4 +68,24 @@ inline fun <T> Iterator<T>.toTreeSet() = to(TreeSet<T>())
/*
Run function f
*/
inline fun <T> run(f: () -> T) = f()
inline fun <T> run(f: () -> T) = f()
/*
Allow a default value to be provided when converting a nullable type to a non-nullable type
*/
inline fun <T> T?.getOrElse(defaultValue: T): T {
return if (this != null)
this
else
defaultValue
}
/*
Allow a default value to be lazily provided from a function when converting a nullable type to a non-nullable type
*/
inline fun <T> T?.getOrElse(defaultValueFactory: ()-> T): T {
return if (this != null)
this
else
defaultValueFactory()
}