Skip to content

Unexpected behavior of NonEmptyList.reduce for tuples of lists #4817

@kiendang

Description

@kiendang

NonEmptyList.reduce used the wrong, or at least unexpected Semigroup instance for tuples of lists. I'm not sure if it's due to cats itself or the compiler though.


Let's say I want to semigroup combine a couple of tuples of lists

Tuple(List(1, 2)).combine(Tuple(List(5)))

I would get Tuple(List(1, 2, 5)), which is expected.

However if I do

NonEmptyList.of(Tuple(List(1, 2)), Tuple(List(5))).reduce

I would get Tuple(<function>). Looks like my tuples of lists not as tuples of List were inferred as tuples of (Int => Int) which is surprising. I would expect the same Semigroup instance would be used in both examples.

I even got

NonEmptyList.of((List(1, 2), List(1, 2), List(5, 6)), (List(4), List(5), List(7))).reduce

to produce Tuple(List(1, 2, 4),List(1, 2, 5),<function1>) on my local machine using scala-cli on Scala 3.7.4 but I couldn't reproduce elsewhere.

Weirder still, this workaround that looks like it would do nothing

def reduce[T: Semigroup](xs: NonEmptyList[T]) =
  xs.reduce

reduce(Tuple(List(1, 2)).combine(Tuple(List(5))))

produces Tuple(List(1, 2, 5)).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions