This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
Provide nullable continuation based on DelimitedScope #251
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dd36538
Provide nullable { } implementation based on DelimitedScope
kioba d04cbc4
provide singleton object with reference check for nested nullable value.
kioba 8075109
Provide extra test cases for nullable.kt
kioba 9eeee59
remove check problems with detekt
kioba d53f447
provide TODO for future fun interface
kioba a08cc87
reduce library visibility for API users.
kioba 6fe7d38
nextShift values doesn't need to be nested nullable value.
kioba 8228524
Fix warning for parameter renaming for overridden method.
kioba b357830
Eliminate the shift call on non-null values
kioba b6fcbbb
move NullableBindSyntax.kt into private inner class for nullable.kt
kioba 0672e90
fix argument ending
kioba 425556c
Merge branch 'master' into master
kioba c8eb55e
trigger GitHub actions
kioba c95827e
Merge branch 'master' into master
kioba 734ea38
trigger GitHub actions
kioba File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
18 changes: 18 additions & 0 deletions
18
arrow-core-data/src/main/kotlin/arrow/core/computations/nullable.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package arrow.core.computations | ||
|
||
import arrow.continuations.generic.DelimContScope | ||
import arrow.continuations.generic.DelimitedScope | ||
import arrow.core.computations.suspended.BindSyntax | ||
|
||
@Suppress("ClassName") | ||
object nullable { | ||
operator fun <A> invoke(func: suspend BindSyntax.() -> A?): A? = | ||
DelimContScope.reset { NullableBindSyntax(this).func() } | ||
|
||
private class NullableBindSyntax<R>( | ||
scope: DelimitedScope<R?> | ||
) : BindSyntax, DelimitedScope<R?> by scope { | ||
override suspend fun <A> A?.invoke(): A = | ||
this ?: shift { null } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
arrow-core-data/src/main/kotlin/arrow/core/computations/suspended/BindSyntax.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package arrow.core.computations.suspended | ||
|
||
/** | ||
* Running A? in the context of [nullable] | ||
* | ||
* ``` | ||
* nullable { | ||
* val one = 1.invoke() // using invoke | ||
* val bigger = (one.takeIf{ it > 1 }).invoke() // using invoke on expression | ||
* bigger | ||
* } | ||
* ``` | ||
*/ | ||
// TODO: this will become interface fun when they support suspend in the next release | ||
interface BindSyntax { | ||
suspend operator fun <A> A?.invoke(): A | ||
} |
61 changes: 61 additions & 0 deletions
61
arrow-core-data/src/test/kotlin/arrow/core/computations/NullableTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package arrow.core.computations | ||
|
||
import arrow.core.test.UnitSpec | ||
import io.kotlintest.shouldBe | ||
|
||
class NullableTest : UnitSpec() { | ||
|
||
init { | ||
"simple case" { | ||
nullable { | ||
"s".length().invoke() | ||
} shouldBe 1 | ||
} | ||
"multiple types" { | ||
nullable { | ||
val number = "s".length() | ||
val string = number.toString()() | ||
string | ||
} shouldBe "1" | ||
} | ||
"short circuit" { | ||
nullable { | ||
val number: Int = "s".length() | ||
(number.takeIf { it > 1 }?.toString())() | ||
throw IllegalStateException("This should not be executed") | ||
} shouldBe null | ||
} | ||
"when expression" { | ||
nullable { | ||
val number = "s".length() | ||
val string = when (number) { | ||
1 -> number.toString() | ||
else -> null | ||
}.invoke() | ||
string | ||
} shouldBe "1" | ||
} | ||
"if expression" { | ||
nullable { | ||
val number = "s".length() | ||
val string = if (number == 1) { | ||
number.toString() | ||
} else { | ||
null | ||
}.invoke() | ||
string | ||
} shouldBe "1" | ||
} | ||
"if expression short circuit" { | ||
nullable { | ||
val number = "s".length() | ||
val string = if (number != 1) { | ||
number.toString() | ||
} else { | ||
null | ||
}() | ||
string | ||
} shouldBe null | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code snippet is more applicable to the nullable object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version of BindSyntax only applies to the nullable implementation usually looking to use the Kinded version. also
NullableBindingSyntax
will be removed once fun interfaces are allowed to have suspendable functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am currently looking into how could we remove the
NullableBindSyntax
until fun interfaces are resolved. There is not much benefit of it outside ofnullable