-
Notifications
You must be signed in to change notification settings - Fork 21
Add the component1 operator to Optional #20
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
Conversation
| is None -> null | ||
| } | ||
|
|
||
| open operator fun component1() = toNullable() |
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.
Let's add javakotlindoc with example?
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.
A README update with example would be great too
|
|
||
| describe("component1") { | ||
|
|
||
| context("Optional<Int>.component1") { |
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.
nit: it's a bit inconsistent with "None.component1"
|
This change introduces funny construct btw: val (lol: Nothing?) = None |
| is None -> null | ||
| } | ||
|
|
||
| open operator fun component1() = toNullable() |
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.
Does it need to be open?
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.
Otherwise it won't compile because Some also declares a component1
| assertThat(result).isNull() | ||
| } | ||
| } | ||
| } |
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.
What about a test with unwrapping in lambda? Something like this:
listOf(Some(42)).forEach { (value) ->
assertThat(value).isEqualTo(42)
}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.
Do we need that though? It's up to compiler right
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.
And Kotlin compiler doesn’t have issues, yep.
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.
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() |
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.
Let's also specify return type explicitly here, that makes it easier for users when they look at sources
| } | ||
| } | ||
|
|
||
| describe("component1") { |
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.
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
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.
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() |
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.
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
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.
nice i like that
|
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 |
|
@AlecStrong can you please update PR with things we've discussed? |
|
@AlecStrong if you don't have bandwidth, I can manually merge it with feedback addressed, wdyt? No pressure though :) |
|
no worries - i was just on vacation but im back now and can update |
|
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 |
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 probably should be more generic and showcase destructuring of both types
and moved above Java Interop section, it's a bit confusing atm haha
|
@AlecStrong we gonna merge it as is and submit additional PR to address remaining feedback to push release sooner :) |
Addresses remaining feedback of PR #20.
Closes #19