kenbot / goggles

Pleasant, yet principled Scala optics DSL
MIT License
195 stars 7 forks source link

Assignment operator sugar for collections #16

Open kenbot opened 7 years ago

kenbot commented 7 years ago

Currently, we have some mildly convenient syntax sugar for updating numeric values:

set"$foo.int" += 3
set"$foo.int" *= 3
set"$foo.int" -= 3

Which translate to

set"$foo.int" ~= (_ + 3)
set"$foo.int" ~= (_ * 3)
set"$foo.int" ~= (_ - 3)

Despite the modest gain, it is appropriate because the operators are so instantly recognisable -- it is not a surprising feature.

It may be similarly justifiable to provide overloads for common collection operators:

set"$foo.list" ::= element
set"$foo.list :::= list
set"$foo.coll" ++= coll
set"$foo.coll" :+= postElement
set"$foo.coll" +:= preElement

These can be annoyingly fiddly with ~= explicit functions.

How should it be implemented? The monocle Cons functionality could work with ::, but none of the others would. If we used the standard Scala CanBuildFrom implicit stuff, then it would have to be consistent, rather than one thing using a Monocle typeclass.

Can it work as expected with polymorphic STAB optics?

Needs more experimentation to decide whether it is possible, or worth it.