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

Update Raise.xOrAccumulate #2958

Merged
merged 18 commits into from
Mar 8, 2023
Merged

Update Raise.xOrAccumulate #2958

merged 18 commits into from
Mar 8, 2023

Conversation

nomisRev
Copy link
Member

@nomisRev nomisRev commented Mar 3, 2023

This PR updates RaiseAccumulate DSL with changes from #2952.

  • It enables binding Nel<E> and E in the same DSL in Raise DSL
  • It optimises the implementation in Raise, and removes a lot of allocations
  • It re-uses the DSL to implement Iterable.mapOrAccumulate

@nomisRev nomisRev added the 1.2.0 Tickets belonging to 1.1.2 label Mar 3, 2023
@nomisRev nomisRev requested a review from serras March 3, 2023 14:40
@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2023

@nomisRev nomisRev changed the title Update RaiseAccumulate Update Raise.xOrAccumulate Mar 4, 2023
@@ -1,4 +1,4 @@
@file:OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class)
Copy link
Member

Choose a reason for hiding this comment

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

No more contracts?

Copy link
Member Author

Choose a reason for hiding this comment

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

I got confused how we should use them, because they resulted in a couple of false-negatives of "Unreachable code" 😕

In this case of catch we should be able to tell the compiler that the second lambda "recovers" from the Nothing of the first lambda. It's a bit strange since the inferred returned type is Int of the catch lambda. So I guess callsInPlace assumes not-wrapped in try?

Screenshot 2023-03-06 at 18 49 39

The second one is more complex.. since either is also callsInPlace and the last statement is an exception, but it's not throwing but forever suspending... And it exists early due to raise 😅

Screenshot 2023-03-06 at 18 43 59

Not sure how we should deal with this best. WDYT @serras?

Copy link
Member Author

@nomisRev nomisRev Mar 6, 2023

Choose a reason for hiding this comment

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

I also noticed that ResultKt only specifies AT_MOST_ONCE and never EXACTLY_ONCE. EXACTLY_ONCE only appears in very little places in Kotlin StdLib 🤔 I also asked this question in KotlinLang #compiler if there is any guidelines for this.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the thorough investigation, I never noticed that before. I assumed that this would help the compiler inline or optimize the code better, but it seems that it's reaching wrong conclusions...

Copy link
Member Author

Choose a reason for hiding this comment

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

I am going to merge this PR, but I am continuing my search for a clear answer on this in Kotlin Slack. Re-asked my question in #library-development, https://kotlinlang.slack.com/archives/C8C4JTXR7/p1678213597746259.

It seems that AT_MOST_ONCE doesn't suffer from the false-negative so I am going to re-add that back before merging.

Base automatically changed from sv-missing-mapOrAccumulate to main March 6, 2023 18:18
@raulraja raulraja closed this Mar 6, 2023
@nomisRev nomisRev reopened this Mar 6, 2023
@nomisRev
Copy link
Member Author

nomisRev commented Mar 6, 2023

Not sure why you closed @raulraja?

@nomisRev nomisRev marked this pull request as ready for review March 6, 2023 18:37
@nomisRev nomisRev requested review from franciscodr, raulraja and a team March 6, 2023 19:01
Comment on lines +435 to +444
val error = mutableListOf<R>()
val results = ArrayList<B>(list.collectionSizeOrDefault(10))
for (item in list) {
fold<NonEmptyList<R>, B, Unit>(
{ transform(RaiseAccumulate(this), item) },
{ errors -> error.addAll(errors) },
{ results.add(it) }
)
}
return error.toNonEmptyListOrNull()?.let { raise(it) } ?: results
Copy link
Member

Choose a reason for hiding this comment

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

The fact that this can be defined now once and for all in a nice way fills me with joy :)

Copy link
Member

@raulraja raulraja left a comment

Choose a reason for hiding this comment

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

great work @nomisRev 🙌

Copy link
Member

@i-walker i-walker left a comment

Choose a reason for hiding this comment

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

Thanks @nomisRev !!

@nomisRev nomisRev merged commit e94dc2e into main Mar 8, 2023
@nomisRev nomisRev deleted the sv-update-raiseaccumulate branch March 8, 2023 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.2.0 Tickets belonging to 1.1.2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants