Skip to content

Commit 74b7841

Browse files
committed
Give implicit conversion on Option function lower priority
1 parent 8e35174 commit 74b7841

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed
File renamed without changes.

shared/src/main/scala/com/thoughtworks/Extractor.scala

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
package com.thoughtworks
22

3+
private[thoughtworks] sealed trait LowPrirorityExtractor {
4+
5+
sealed trait SeqExtractor[-A, +B] {
6+
def unapplySeq(a: A): Option[Seq[B]]
7+
}
8+
9+
implicit final class OptionFunctionToSeqExtractor[-A, +B] private[LowPrirorityExtractor](underlying: A => Option[Seq[B]]) {
10+
def extractSeq = new SeqExtractor[A, B] {
11+
def unapplySeq(a: A) = underlying(a)
12+
}
13+
}
14+
15+
implicit final class OptionFunctionToExtractor[-A, +B] private[LowPrirorityExtractor](underlying: A => Option[B]) {
16+
def extract = new Extractor[A, B] {
17+
def unapply(a: A) = underlying(a)
18+
}
19+
}
20+
21+
}
22+
23+
/**
24+
* A pattern that can be used in `match` / `case` expressions.
25+
*/
26+
sealed trait Extractor[-A, +B] {
27+
def unapply(a: A): Option[B]
28+
}
29+
330
/**
431
* Utilities to convert between `A => Option[B]`, `PartialFunction[A, B]` and [[Extractor]].
532
*
@@ -37,47 +64,24 @@ package com.thoughtworks
3764
}}}
3865
*
3966
*/
40-
object Extractor {
41-
42-
sealed trait SeqExtractor[-A, +B] {
43-
def unapplySeq(a: A): Option[Seq[B]]
44-
}
67+
object Extractor extends LowPrirorityExtractor {
4568

4669
implicit final class PartialFunctionToSeqExtractor[-A, +B] private[Extractor](underlying: PartialFunction[A, Seq[B]]) {
4770
def extractSeq = new SeqExtractor[A, B] {
4871
def unapplySeq(a: A) = underlying.lift(a)
4972
}
5073
}
5174

52-
implicit final class OptionFunctionToSeqExtractor[-A, +B] private[Extractor](underlying: A => Option[Seq[B]]) {
53-
def extractSeq = new SeqExtractor[A, B] {
54-
def unapplySeq(a: A) = underlying(a)
55-
}
56-
}
57-
5875
implicit final class PartialFunctionToExtractor[-A, +B] private[Extractor](underlying: PartialFunction[A, B]) {
5976
def extract = new Extractor[A, B] {
6077
def unapply(a: A) = underlying.lift(a)
6178
}
6279
}
6380

64-
implicit final class OptionFunctionToExtractor[-A, +B] private[Extractor](underlying: A => Option[B]) {
65-
def extract = new Extractor[A, B] {
66-
def unapply(a: A) = underlying(a)
67-
}
68-
}
69-
7081
implicit final class OptionFunctionToPartialFunction[-A, +B] private[Extractor](underlying: A => Option[B]) {
7182
def unlift: PartialFunction[A, B] = {
7283
case underlying.extract(b) => b
7384
}
7485
}
7586

76-
}
77-
78-
/**
79-
* A pattern that can be used in `match` / `case` expressions.
80-
*/
81-
sealed trait Extractor[-A, +B] {
82-
def unapply(a: A): Option[B]
83-
}
87+
}

shared/src/test/scala/com/thoughtworks/ExtractorSpec.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class ExtractorSpec extends FreeSpec with Matchers {
3131
}
3232
}
3333

34+
"PartialOptionFunction toExtractor" in {
35+
36+
val pf: PartialFunction[Int, Option[String]] = {
37+
case 1 => Some("match")
38+
}
39+
40+
1 match {
41+
case pf.extract(m) => m should be(Some("match"))
42+
}
43+
44+
}
45+
3446
"example" in {
3547
val pf: PartialFunction[Int, String] = {
3648
case 1 => "matched by PartialFunction"

0 commit comments

Comments
 (0)