Skip to content

Type mismatch when unapply returns a type parameterized with a singleton type #13169

@noresttherein

Description

@noresttherein

Reproduction steps

Scala version: 2.13.16

	trait Contains[-C, -X] {
		def apply(items :C, item :X) :Boolean
	}
	object SetLike {
		def unapply[X](items :IterableOnce[X]) :Option[Contains[items.type, X]] =
			items match {
				case set :collection.Set[X @unchecked] => Some(
					(new Contains[collection.Set[X], X] {
						override def apply(items :collection.Set[X], item :X) :Boolean = items(item)
					}).asInstanceOf[Contains[items.type, X]]
				)
				case _ => None
			}
	}

	def contains[X](items :IterableOnce[X], item :X) :Boolean = items match {
		case SetLike(contains) => contains(items, item)
		case _ => SetLike.unapply(items) match {
			case Some(contains) => contains(items, item)
			case _              => items.iterator.contains(item)
		}
	}

Problem

type mismatch;
 found   : items.type (with underlying type IterableOnce[X])
 required: <unapply-selector>.type
		case SetLike(contains) => contains(items, item)

Calling unapply directly and then matching a value already parameterized with a singleton type, as in the second case in contains works as intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions