Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@AlecKazakova
Copy link
Contributor

Closes #19

is None -> null
}

open operator fun component1() = toNullable()
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add javakotlindoc with example?

Copy link
Contributor

Choose a reason for hiding this comment

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

A README update with example would be great too


describe("component1") {

context("Optional<Int>.component1") {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: it's a bit inconsistent with "None.component1"

@artem-zinnatullin
Copy link
Contributor

This change introduces funny construct btw:

val (lol: Nothing?) = None

is None -> null
}

open operator fun component1() = toNullable()
Copy link
Member

Choose a reason for hiding this comment

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

Does it need to be open?

Copy link
Contributor

Choose a reason for hiding this comment

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

Otherwise it won't compile because Some also declares a component1

assertThat(result).isNull()
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

What about a test with unwrapping in lambda? Something like this:

listOf(Some(42)).forEach { (value) ->
    assertThat(value).isEqualTo(42)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need that though? It's up to compiler right

Copy link
Member

Choose a reason for hiding this comment

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

And Kotlin compiler doesn’t have issues, yep.

Copy link
Member

Choose a reason for hiding this comment

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

It can serve as a checking sample as well, since it is a more desirable usage (at least for me).

is None -> null
}

open operator fun component1() = toNullable()
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's also specify return type explicitly here, that makes it easier for users when they look at sources

}
}

describe("component1") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add few tests to check type system behavior?

I was afraid that this PR would break previous construction:

val (value: String) = Some("str")

Because now Optional declares component1() as T?.

But gladly compiler actually generates proper function override in Some with @NotNull:

class Some extends Optional {
   @NotNull
   public final Object component1() {
      return this.value;
   }

So we get both T? if you destructure Optional and T if you destructure Some<T>, but I'm afraid that this behavior can be easily broken in compiler and that there are not so many similar use cases to report that to them quickly

Copy link
Contributor

Choose a reason for hiding this comment

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

Something like:

context("destructure Some") {

    val some = Some("string")

    it("destructures to non-null type") {
        val (value: String) = some
    }
}

But for all types: Optional, Some, None. Right now you test behavior but not the type information

is None -> null
}

open operator fun component1() = toNullable()
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, playing more with it I think we should define it as an abstract function:

sealed class Optional {
    abstract operator fun component1(): T?
}

// `Some` overrides it automatically, 
// also looks like we can't define it manually in either case which worries me a bit

object None : Optional<Nothing> {
    override fun component1(): Nothing? = null
}

That will work faster for None because it will just return null without doing instanceof that toNullable() does

And actual behavior for Some will be more clear because right now it's not obvious that toNullable() is not actually executed due to override generated by compiler

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice i like that

@artem-zinnatullin
Copy link
Contributor

jeez, Koptional is like 3 types and 2 functions and we already have one PR that breaks compatibility and now this one is actually not that simple even though it adds just one function hahaha I just can't killme

@artem-zinnatullin
Copy link
Contributor

@AlecStrong can you please update PR with things we've discussed?

@artem-zinnatullin
Copy link
Contributor

@AlecStrong if you don't have bandwidth, I can manually merge it with feedback addressed, wdyt?

No pressure though :)

@AlecKazakova
Copy link
Contributor Author

no worries - i was just on vacation but im back now and can update

@artem-zinnatullin
Copy link
Contributor

Niiice! A vacation would be so nice…

// Can you pls resolve conflicts?

Use the static `Optional.toOptional()` method (declared as a companion object method) to wrap an
instance of `T` into `Optional<T>`.

#### Destructure `Some<T>` into T
Copy link
Contributor

Choose a reason for hiding this comment

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

This probably should be more generic and showcase destructuring of both types

and moved above Java Interop section, it's a bit confusing atm haha

@artem-zinnatullin
Copy link
Contributor

@AlecStrong we gonna merge it as is and submit additional PR to address remaining feedback to push release sooner :)

@artem-zinnatullin artem-zinnatullin merged commit 5d49dc2 into gojuno:master Jun 13, 2018
artem-zinnatullin added a commit that referenced this pull request Jun 13, 2018
Addresses remaining feedback of PR #20.
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.

3 participants