Lenses: Composable Getters and Setters for Functional Programming
medium.com
medium.com
* First, it's possible that different elements of an array or different properties of an object has different shapes. I can't imagine how lenses can deal with that in an idiomatic way.
* Second, it's just difficult to get things right without a compiler's help. Forget a `traverse`? Now you are operating nonsense on the wrong level of your nested data structure. Trying to modify an inner structure but you only have a Fold instead of a Traversal? You won't know it won't work without running it to get some cryptic error.
* Furthermore, without types, we obscure the fact that setters and getters are unified: if `a` stands for the part and `s` for the whole, we just have
(a -> f a) -> (s -> f s)
that is, given a way to transform some inner part, we can transform the whole as well. Getters and setters are just supplying a concrete choice of f, namely the const functor and the identity functor. If we pick f to be the identity functor, then with some unwrapping we have (a -> a) -> (s -> s)
so if there is a function that can modify the part, we have a function that can modify the whole by keeping the rest the same and modify that part. If we pick f to be the const functor, then with some unwrapping we have (a -> r) -> (s -> r)
so if there is a function that can extract the value from the part, we can extract the value from the whole as well. Without types, this rather elegant unification of getter and setter seems to be lost.> First, it's possible that different elements of an array or different properties of an object has different shapes
I can't imagine how this would introduce any sort of difficulties w.r.t. lenses that aren't already present with normal getting and setting of nested structures (aka probably not much).
> Second, it's just difficult to get things right without a compiler's help
This has nothing to do with lenses and everything to do with you preferring the feel of static types. You go on to talk about a bunch of issues with complex constructs that really only exist in static typing.
Sometimes you write a wrong thing and get a runtime error. This happens in all dynamically typed languages (and many statically typed as well).
> Furthermore, without types... this rather elegant unification of getter and setter seems to be lost.
Like ya do. I don't see how this has any impact at all on developing applications.
I'm pretty sure it isn't types that are causing the aesthetic and usability differences you are seeing. I feel the same way about looking at js and typescript lens use. I thought it was just the result of using a concept from one paradigm in a language designed for another, which caused some awkwardness.
This is way beyond what most people need to know to benefit from lens though.
Haskell itself has a counterexample: aeson-lens. It gives you lenses and prisms for working with JSON ASTs. Any unityped language could benefit from lenses for their unityped structures (JS and Clojure come to mind.)
Assuming you are in a unityped context, hkts are gone, functor is not a thing. so
(a -> f a) -> (s -> f s)
is not meaningful. If we pretend that unityped means everything is parametrically polymorphic, for the purpose of this discussion, can't we leverage parametricity to choose a representation that provides this unification?for example, if i provide laws that show for a given
lens :: (s -> a, s -> a -> s)
that (snd lens) s (fst lens $ s) == id s
doesn't that provide the same assurance, even if less elegant and not typechecked (the latter of which is much broader than just applicability of lenses)The author of the post is also the author of a TypeScript port of Monocle for Scala.
How do you pass a member identification as a parameter to a function that may receive any kind of structure, and then update that member?
Haskell has record accessors too, that are more equivalent to the C functionality. Lenses are a different thing.
In C++, you forward responsibility for knowing object shape down the line of composition. You also generally avoid deep copies because they are expensive. This leads to using getter/setter methods to abstract object shape at module boundaries. If you're dealing with immutable objects, then you bake that into the interface (such as std::string).
If you were deep copying objects in C++, first I would ask whether you really need to. If you really did, then the idiomatic way is to either provide a function like ".clone" or to implement the copy constructor to do it.
How do you pass a member identification as a parameter to a function that may receive any kind of structure, and then update that member?
You don't, because that's not something C++ as a language can do. So instead, the question is how you got into a situation in C++ where you need to.
On the point about composability, in C++ the composition happens at the object level. You don't compose getting/setting functions because that's not how C++ addresses the same problem.
You don't. You just update that member.
The motivation on the article is basically this paragraph:
"Lenses allow you to abstract state shape behind getters and setters. Instead of littering your codebase with code that dives deep into the shape of a particular object, import a lens. If you later need to change the state shape, you can do so in the lens, and none of the code that depends on the lens will need to change."
Immutability does not make its way there.
Somehow i sense this code has arguments in the wrong order.