Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/arrowcontracts #834

Merged
merged 3 commits into from
Jun 17, 2019
Merged

Feature/arrowcontracts #834

merged 3 commits into from
Jun 17, 2019

Conversation

sksamuel
Copy link
Member

@sksamuel sksamuel commented Jun 15, 2019

I've added contracts for Validated, Either and Option.
These contracts allow us to extract the field from these datatypes, but because of the issue with contracts not supporting generics yet, the extracted field will be Any?.
This is still a big improvement I think, and also means when contracts support generics (1.4?) we can easily update these matchers.

Current example:

val either = "foo".right()
either.shouldBeRight()
(either as Either.Right<String>).b shouldBe "foo"
(either as Either.Right<String>).b.length shouldBe 3

Example with this PR:

val either = "foo".right()
either.shouldBeRight()
either.b shouldBe "foo"
(either.b as String).length shouldBe 3

Example in the future:

val either = "foo".right()
either.shouldBeRight()
either.b shouldBe "foo"
either.b.length shouldBe 3

This partially addresses the feature request of #612

@@ -23,4 +23,11 @@ dependencies {
testCompile project(":kotlintest-runner:kotlintest-runner-junit5")
}

compileKotlin {
kotlinOptions {
freeCompilerArgs += '-Xuse-experimental=kotlin.Experimental'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually necessary?

I remember adding a experimental contract somewhere else in our code, and I don't think I needed to add this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you need it where the contract is defined, but not where it is used.

@UseExperimental(ExperimentalContracts::class)
fun <T> Either<*, T>.shouldBeRight() {
contract {
returns() implies (this@shouldBeRight is Either.Right<*>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted this to be the generic type, but it's not possible due to contracts not supporting it yet, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so once contracts support generics we can just change Either.Right<*> to Either.Right<T> and it will be generic.

@LeoColman
Copy link
Member

LeoColman commented Jun 17, 2019 via email

@sksamuel
Copy link
Member Author

sksamuel commented Jun 17, 2019 via email

@LeoColman LeoColman merged commit e99ede8 into master Jun 17, 2019
@LeoColman LeoColman deleted the feature/arrowcontracts branch June 17, 2019 17:13
@sksamuel sksamuel added this to the 3.4 milestone Jul 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants