I love lenses and other optics like prisms and folds, but only in a statically typed language like Haskell. It seems to me that it would be very hard to get things right in a dynamically typed language:<p>* 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.<p>* 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.<p>* 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<p><pre><code> (a -> f a) -> (s -> f s)
</code></pre>
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<p><pre><code> (a -> a) -> (s -> s)
</code></pre>
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<p><pre><code> (a -> r) -> (s -> r)
</code></pre>
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.