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

["Request"] Ease NonEmptyCollection methods usage calling suspend #3116

Closed
christophejan opened this issue Aug 18, 2023 · 3 comments · Fixed by #3120
Closed

["Request"] Ease NonEmptyCollection methods usage calling suspend #3116

christophejan opened this issue Aug 18, 2023 · 3 comments · Fixed by #3120

Comments

@christophejan
Copy link

What version are you currently using?

1.2.0

What would you like to see?

Currently, the following mapIndexedOnNonEmptyList method get a Suspension functions can be called only within coroutine body compilation error, but not mapIndexedOnStandardCollection :

suspend fun mySuspendFunction(index: Int, value: Int): Any = TODO()

suspend fun mapIndexedOnNonEmptyList() =
    nonEmptyListOf(1, 3, 7).mapIndexed { index: Int, value: Int -> mySuspendFunction(index, value) }

suspend fun mapIndexedOnStandardCollection() =
    listOf(1, 3, 7).mapIndexed { index: Int, value: Int -> mySuspendFunction(index, value) }

I would like to be able to use NonEmptyCollection methods in a suspend method definition the same way you can on standard collections.

(note that the standard collection behavior is due to inline method definition of Iterable<T>.mapIndexed)

Why?

I think that it would ease NonEmptyCollection usage (to avoid custom extensions or to avoid switching back on standard collections).

@serras
Copy link
Member

serras commented Aug 23, 2023

The problem here is that NonEmptyCollection defines map and friends as member methods. We could define them as extension functions, but this has two drawbacks:

  1. We cannot provide "direct" implementations for things like NonEmptyList,
  2. We would break source compatibility, since now users would need to import map explicitly.

What do you think? Summoning also @nomisRev @raulraja @franciscodr to the conversation.

@nomisRev
Copy link
Member

nomisRev commented Aug 28, 2023

Hmm, we can not solve this for the NonEmptyCollection type itself, but I think we can solve it for the concrete APIs by doing what we did for map. This comes at the cost of more code in Arrow, but it doesn't impact the binary size afaik.

@Suppress("OVERRIDE_BY_INLINE")
public override inline fun <B> map(transform: (A) -> B): NonEmptyList<B> =
  NonEmptyList(transform(head), tail.map(transform))

This works for example, whilst map is also defined for NonEmptyCollection.

public suspend fun mySuspendFunction(value: Int): Int = TODO()

public suspend fun mapIndexedOnNonEmptyList(): NonEmptyList<Int> =
  nonEmptyListOf(1, 3, 7).map { value: Int -> mySuspendFunction(value) }

@christophejan
Copy link
Author

Thank You @serras @nomisRev

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 a pull request may close this issue.

3 participants