Skip to content

Bug: NonEmptyList derived directly from MutableList propagates changes to #all property, but not #size, #head and #tail #1312

@billwanjohi

Description

@billwanjohi

I created a NonEmptyList by passing a standard Kotlin MutableList to it. I had assumed that this new variable was fully immutable, implicitly making a copy of the mutable list and severing any connection to it. Instead, what I found is that it continues to reference the mutable list for at least one property (all), but not for others.

val mutableList = mutableListOf(5, 8, 13)
NonEmptyList.fromList(mutableList).map { nonEmptyList ->
    assert(nonEmptyList.all == listOf(5, 8, 13))
    mutableList.clear()
    assert(nonEmptyList.all == emptyList<Int>())  // Yikes
    assert(nonEmptyList.size == 3)
    assert(nonEmptyList.head == 5)
    assert(nonEmptyList.tail == listOf(8, 13))
}                                                       

My workaround is to call #toList on the MutableList first, but hopefully you agree that this is a bug, and the mutable list should either not be rejected or have consistent effects in all properties and elements. As it is, it makes NonEmptyList more dangerous to use in my context than the theoretically looser Kotlin List.

I'm running arrow v0.8.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions