diff --git a/core/src/main/scala/cats/data/NonEmptyVector.scala b/core/src/main/scala/cats/data/NonEmptyVector.scala index dcc7f4fb98..9da8c5e0b4 100644 --- a/core/src/main/scala/cats/data/NonEmptyVector.scala +++ b/core/src/main/scala/cats/data/NonEmptyVector.scala @@ -223,6 +223,8 @@ object NonEmptyVector extends NonEmptyVectorInstances { new NonEmptyVector(buf.result) } + def unapply[A](nev: NonEmptyVector[A]): Some[(A, Vector[A])] = Some((nev.head, nev.tail)) + def fromVector[A](vector: Vector[A]): Option[NonEmptyVector[A]] = if (vector.isEmpty) None else Some(new NonEmptyVector(vector)) diff --git a/tests/src/test/scala/cats/tests/NonEmptyVectorTests.scala b/tests/src/test/scala/cats/tests/NonEmptyVectorTests.scala index ca5c99ce7f..1cc6e4ca6e 100644 --- a/tests/src/test/scala/cats/tests/NonEmptyVectorTests.scala +++ b/tests/src/test/scala/cats/tests/NonEmptyVectorTests.scala @@ -243,6 +243,16 @@ class NonEmptyVectorTests extends CatsSuite { NonEmptyVector(1, Vector.empty).toVector.toString should === ("Vector(1)") } + test("NonEmptyVector.unapply supports pattern matching") { + forAll { (nonEmptyVector: NonEmptyVector[Int]) => + nonEmptyVector match { + case NonEmptyVector(head, tail) => + head should === (nonEmptyVector.head) + tail should === (nonEmptyVector.tail) + } + } + } + test("Cannot create a new NonEmptyVector from constructor") { if(Platform.isJvm) { if (!Properties.versionNumberString.startsWith("2.10")) {