@@ -3,7 +3,8 @@ package monocle.internal.focus
3
3
import monocle .internal .focus .features .fieldselect .FieldSelectGenerator
4
4
import monocle .internal .focus .features .optionsome .OptionSomeGenerator
5
5
import monocle .internal .focus .features .castas .CastAsGenerator
6
- import monocle .{Lens , Iso , Prism , Optional }
6
+ import monocle .internal .focus .features .each .EachGenerator
7
+ import monocle .{Lens , Iso , Prism , Optional , Traversal }
7
8
import scala .quoted .Type
8
9
9
10
@@ -12,6 +13,7 @@ private[focus] trait AllGenerators
12
13
with FieldSelectGenerator
13
14
with OptionSomeGenerator
14
15
with CastAsGenerator
16
+ with EachGenerator
15
17
16
18
private [focus] trait GeneratorLoop {
17
19
this : FocusBase with AllGenerators =>
@@ -31,60 +33,14 @@ private[focus] trait GeneratorLoop {
31
33
case FocusAction .FieldSelect (name, fromType, fromTypeArgs, toType) => generateFieldSelect(name, fromType, fromTypeArgs, toType)
32
34
case FocusAction .OptionSome (toType) => generateOptionSome(toType)
33
35
case FocusAction .CastAs (fromType, toType) => generateCastAs(fromType, toType)
36
+ case FocusAction .Each (fromType, toType, eachInstance) => generateEach(fromType, toType, eachInstance)
34
37
}
35
38
36
39
private def composeOptics (lens1 : Term , lens2 : Term ): FocusResult [Term ] = {
37
- (lens1.tpe.asType, lens2.tpe.asType) match {
38
- case (' [Lens [from1, to1]], ' [Lens [from2, to2]]) =>
39
- Right (' { $ {lens1.asExprOf[Lens [from1, to1]]}.andThen($ {lens2.asExprOf[Lens [to1, to2]]}) }.asTerm)
40
-
41
- case (' [Lens [from1, to1]], ' [Prism [from2, to2]]) =>
42
- Right (' { $ {lens1.asExprOf[Lens [from1, to1]]}.andThen($ {lens2.asExprOf[Prism [to1, to2]]}) }.asTerm)
43
-
44
- case (' [Lens [from1, to1]], ' [Optional [from2, to2]]) =>
45
- Right (' { $ {lens1.asExprOf[Lens [from1, to1]]}.andThen($ {lens2.asExprOf[Optional [to1, to2]]}) }.asTerm)
46
-
47
- case (' [Lens [from1, to1]], ' [Iso [from2, to2]]) =>
48
- Right (' { $ {lens1.asExprOf[Lens [from1, to1]]}.andThen($ {lens2.asExprOf[Iso [to1, to2]]}) }.asTerm)
49
-
50
- case (' [Prism [from1, to1]], ' [Prism [from2, to2]]) =>
51
- Right (' { $ {lens1.asExprOf[Prism [from1, to1]]}.andThen($ {lens2.asExprOf[Prism [to1, to2]]}) }.asTerm)
52
-
53
- case (' [Prism [from1, to1]], ' [Lens [from2, to2]]) =>
54
- Right (' { $ {lens1.asExprOf[Prism [from1, to1]]}.andThen($ {lens2.asExprOf[Lens [to1, to2]]}) }.asTerm)
55
-
56
- case (' [Prism [from1, to1]], ' [Optional [from2, to2]]) =>
57
- Right (' { $ {lens1.asExprOf[Prism [from1, to1]]}.andThen($ {lens2.asExprOf[Optional [to1, to2]]}) }.asTerm)
58
-
59
- case (' [Prism [from1, to1]], ' [Iso [from2, to2]]) =>
60
- Right (' { $ {lens1.asExprOf[Prism [from1, to1]]}.andThen($ {lens2.asExprOf[Iso [to1, to2]]}) }.asTerm)
61
-
62
- case (' [Optional [from1, to1]], ' [Lens [from2, to2]]) =>
63
- Right (' { $ {lens1.asExprOf[Optional [from1, to1]]}.andThen($ {lens2.asExprOf[Lens [to1, to2]]}) }.asTerm)
64
-
65
- case (' [Optional [from1, to1]], ' [Optional [from2, to2]]) =>
66
- Right (' { $ {lens1.asExprOf[Optional [from1, to1]]}.andThen($ {lens2.asExprOf[Optional [to1, to2]]}) }.asTerm)
67
-
68
- case (' [Optional [from1, to1]], ' [Prism [from2, to2]]) =>
69
- Right (' { $ {lens1.asExprOf[Optional [from1, to1]]}.andThen($ {lens2.asExprOf[Prism [to1, to2]]}) }.asTerm)
70
-
71
- case (' [Optional [from1, to1]], ' [Iso [from2, to2]]) =>
72
- Right (' { $ {lens1.asExprOf[Optional [from1, to1]]}.andThen($ {lens2.asExprOf[Iso [to1, to2]]}) }.asTerm)
73
-
74
- case (' [Iso [from1, to1]], ' [Lens [from2, to2]]) =>
75
- Right (' { $ {lens1.asExprOf[Iso [from1, to1]]}.andThen($ {lens2.asExprOf[Lens [to1, to2]]}) }.asTerm)
76
-
77
- case (' [Iso [from1, to1]], ' [Iso [from2, to2]]) =>
78
- Right (' { $ {lens1.asExprOf[Iso [from1, to1]]}.andThen($ {lens2.asExprOf[Iso [to1, to2]]}) }.asTerm)
79
-
80
- case (' [Iso [from1, to1]], ' [Optional [from2, to2]]) =>
81
- Right (' { $ {lens1.asExprOf[Iso [from1, to1]]}.andThen($ {lens2.asExprOf[Optional [to1, to2]]}) }.asTerm)
82
-
83
- case (' [Iso [from1, to1]], ' [Prism [from2, to2]]) =>
84
- Right (' { $ {lens1.asExprOf[Iso [from1, to1]]}.andThen($ {lens2.asExprOf[Prism [to1, to2]]}) }.asTerm)
85
-
86
- case (' [a], ' [b]) =>
87
- FocusError .ComposeMismatch (TypeRepr .of[a].show, TypeRepr .of[b].show).asResult
40
+ lens2.tpe.widen match {
41
+ // Won't yet work for polymorphism where A != B, nor for non-polymorphic optics Getter, Setter or Fold.
42
+ case AppliedType (_, List (_, toType2)) => Right (Select .overloaded(lens1, " andThen" , List (toType2, toType2), List (lens2)))
43
+ case _ => FocusError .ComposeMismatch (lens1.tpe.show, lens2.tpe.show).asResult
88
44
}
89
45
}
90
46
}
0 commit comments