You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is a lens. Essentially the lens interface is very very simple:
mixin Lens<T,K>{
//Gets field from parent
K get(T t);
//Updates parent to set field to the value
T update(T t,K k);
Lens<T,C> then<C>(Lens<K,C> lens);
}
The beauty of lenses is that you can compose them. And the best thing is that you can use them with widgets in flutter. Suppose you have a state somewhere in your application, and in that state you have a list of some objects. You also have a widget that can update objects of that type. But your widget actually doesn't care where it sends the changes to. Lenses allow this abstraction. They just allow you to edit a value of the type you're interested in somewhere. Imagine a complicated nested object. Then you can for example say: Department$.manager.then(Manager$.employees).then(List$.index(0)).then(Employee$.salary). If you then combine the lens with a getter and setter for the source object (for example a Cubit) you can give widgets a place to put their modifications. This eliminates the need to say in all your Cubits : setFieldX(X x) => emit(state.copyWith(x : x).
Generating lenses is very simple, the only thing required would be generating a class as follows:
Lenses are a concept from functional programming that essentially focus a field of an object. A clear example is:
What is a lens. Essentially the lens interface is very very simple:
The beauty of lenses is that you can compose them. And the best thing is that you can use them with widgets in flutter. Suppose you have a state somewhere in your application, and in that state you have a list of some objects. You also have a widget that can update objects of that type. But your widget actually doesn't care where it sends the changes to. Lenses allow this abstraction. They just allow you to edit a value of the type you're interested in somewhere. Imagine a complicated nested object. Then you can for example say: Department$.manager.then(Manager$.employees).then(List$.index(0)).then(Employee$.salary). If you then combine the lens with a getter and setter for the source object (for example a Cubit) you can give widgets a place to put their modifications. This eliminates the need to say in all your Cubits : setFieldX(X x) => emit(state.copyWith(x : x).
Generating lenses is very simple, the only thing required would be generating a class as follows:
I added type arguments for the case the class has them. The generation is simple and the feature would be very powerful
The text was updated successfully, but these errors were encountered: