@@ -21,29 +21,35 @@ object Lens {
21
21
def setterBody (obj : Expr [S ], value : Expr [T ], field : String ): Expr [S ] =
22
22
Term .Select .overloaded(obj.unseal, " copy" , Nil , Term .NamedArg (field, value.unseal) :: Nil ).seal[S ]
23
23
24
- getter.unseal.underlyingArgument match {
25
- case Term .Block (
26
- DefDef (_, Nil , (param :: Nil ) :: Nil , _, Some (Term .Select (o, field))) :: Nil ,
27
- Term .Lambda (meth, _)
28
- ) =>
24
+ // exception: getter.unseal.underlyingArgument
25
+ getter.unseal match {
26
+ case Term .Inlined (
27
+ None , Nil ,
28
+ Term .Block (
29
+ DefDef (_, Nil , (param :: Nil ) :: Nil , _, Some (Term .Select (o, field))) :: Nil ,
30
+ Term .Lambda (meth, _)
31
+ )
32
+ ) if o.symbol == param.symbol =>
29
33
' {
30
34
val setter = (t : T ) => (s : S ) => ~ setterBody('(s), ' (t), field)
31
35
apply(~ getter)(setter)
32
36
}
37
+ case _ =>
38
+ throw new QuoteError (" Unsupported syntax. Example: `GenLens[Address](_.streetNumber)`" )
33
39
}
34
40
}
35
41
}
36
42
37
43
object GenLens {
38
44
/** case class Address(streetNumber: Int, streetName: String)
39
45
*
40
- * Lens.gen [Address, Int ](_.streetNumber) ~~>
46
+ * GenLens [Address](_.streetNumber) ~~>
41
47
*
42
48
* Lens[Address, Int](_.streetNumber)(n => a => a.copy(streetNumber = n))
43
49
*/
44
50
45
51
def apply [S ] = new MkGenLens [S ]
46
52
class MkGenLens [S ] {
47
- inline def apply [T ](get : S => T ): Lens [S , T ] = ~ Lens .impl('(get))
53
+ inline def apply [T ](get : => ( S => T ) ): Lens [S , T ] = ~ Lens .impl('(get))
48
54
}
49
55
}
0 commit comments