Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

["Request"] Add a fold function for nullable types #241

Open
LordRaydenMK opened this issue Sep 27, 2020 · 4 comments
Open

["Request"] Add a fold function for nullable types #241

LordRaydenMK opened this issue Sep 27, 2020 · 4 comments

Comments

@LordRaydenMK
Copy link
Contributor

What version are you currently using?

0.11

What would you like to see?

When moving from Option to using nullable types, the fold function is missing so you end up doing:

?.let { ... } ?: run { ... }

We can add a function:

fun <A, B> A?.fold(ifNull: () -> B, ifPresent: (A) -> B): B =
    this?.let(ifPresent) ?: ifNull()

that would make this a bit easier to work with.

Source: #239 (comment) suggested by @pablisco

You can also see the pattern in the PR.

@pablisco
Copy link
Contributor

If we mirror the current Option API for nullables it will make migration much easier as we can use ReplaceWith without any compilation errors after the replacement. Aditionally, having map, flatMap and such would be a nice companion to stdlib

@raulraja
Copy link
Member

Can we make these inline?

@pablisco
Copy link
Contributor

pablisco commented Oct 2, 2020

After more time than I would like to admit, I realised one problem...
A? is contravariant of all Kinded types, so it adds ambiguity and, for obvious reasons, it cannot be resolved 🤦

Maybe if we call it foldNull? I'm uncertain if there will be inline classes in the future. This indicates that could be (maybe?) https://github.com/Kotlin/KEEP/blob/master/proposals/inline-classes.md#inline-classes-over-nullable-reference-types

That would be the ideal solution, in reality, so we can avoid polluting the global scope with extension functions like these.

@nomisRev
Copy link
Member

nomisRev commented Dec 20, 2020

Yes, we currently also have this issue with mapN defined in Nullable.kt it's conflicing with mapN for List and possibly other top-level methods such as mapN, tupledN we might want to add for other types. I don't think it makes sense to add such extensions on A?, but arity builders functions like mapN which require a lot of boilerplate I do think it makes sense.

this?.let(ifPresent) ?: ifNull() is more Kotlin idiomatic than this.fold(ifPresent, ifNull).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants