Re-think in light of protocol extensions
Now that protocols can have concrete implementations, we should re-visit whether Few can be designed around value types and protocols. :sparkles:
This would be a huge improvement! I did a little proof of concept and here are the results:
Types
LayoutPropertiesstruct that has all information about how to layout the ElementLayoutableprotocol has a mutable({ get set }) LayoutProperties propertyElementTypeprotocol extends Layoutable and has all the methods required to do the renderingParentElementTypeprotocol for Element's that has children.Element: a plain implementation ofElementTypeComponent<State>: only difference is that it now conform's toElementTypeinstead of extendingElement
Extensions
the idea is that most of the methods on Element can be added as extension of Layoutable or ParentElementType.
- For Layoutable
func width(width: CGFloat) -> Selffunc height(height: CGFloat) -> Selffunc direction(direction: Direction) -> Selffunc flex(flex: CGFloat) -> Selffunc childAlignment(alignment: ChildAlignment) -> Selffunc selfAlignment(alignment: SelfAlignment) -> Selffunc justification(justification: Justification) -> Selffunc margin(edges: Edges) -> Selffunc size(width: CGFloat, _ height: CGFloat) -> Self
- For
ParentElementTypefunc children(children: [ElementType]) -> Self
Challenges
- We must avoid associated values in our protocol's. Associated values force you to use generics to infer the type of the children array and that is problematic if you want multiple different elements as your children. I don't know how we are going to tackle this, currently
applyDiffhas a propertySelfand that also requires to infer the type using generics. - We can't use
mutatingfunctions if we use protocol extensions
But there's a workaround for this:

This is just to start a discussion about how it's all going to work.
This is great, thanks for starting to think through it!
I'm wondering what the motivation is for separating ElementType and Layoutable? It seems unlikely we'd need one without the other.
I'm wondering what the motivation is for separating ElementType and Layoutable? It seems unlikely we'd need one without the other.
Mostly because this was a proof of concept and the first thing i wanted to do was add protocol extensions for mutating the layout properties. I agree, we don't need Layoutable.