Paralayout's applyVerticalSubviewDistribution
and applyHorizontalSubviewDistribution
methods are incredibly powerful. Adding a result builder could make the code that creates these distributions more expressive.
In the simplest of cases, utilizing a distribution builder would enable removing the need to declare an array. Taking us from:
containerView.applyVerticalSubviewDistribution(
[
1.flexible,
titleLabel,
16.fixed,
bodyLabel,
16.fixed,
actionButton,
1.flexible,
]
)
to:
containerView.applyVerticalSubviewDistribution {
1.flexible
titleLabel
16.fixed
bodyLabel
16.fixed
actionButton
1.flexible
}
But that's just the beginning. Since result builders allow for inline if
statements, they'd enable writing distribution like:
containerView.applyVerticalSubviewDistribution {
1.flexible
titleLabel
if bodyLabel.hasText {
16.fixed
bodyLabel
}
16.fixed
actionButton
1.flexible
}
Result builders also enable inlining for loop and if-else statements, enabling consumers to inline complex distributions. The benefits here purely syntactic – there'd be zero changes required to the underlying Paralayout library. The only additions would be:
- A
@resultbuilder public struct ViewDistributionBuilder
type.
- A
applyVerticalSubviewDistribution
method that takes a @ViewDistributionBuilder _ distribution: () -> [ViewDistributionSpecifying]
closure rather than a _ distribution: [ViewDistributionSpecifying]
argument. This new method would call through to the existing applyVerticalSubviewDistribution
method.
- A
applyHorizontalSubviewDistribution
method that takes a @ViewDistributionBuilder _ distribution: () -> [ViewDistributionSpecifying]
closure rather than a _ distribution: [ViewDistributionSpecifying]
argument. This new method would call through to the existing applyVerticalSubviewDistribution
method.
Result builders were introduced in Swift 5.4, so this functionality would only be added for consumers using Swift 5.4 and above. I'd be happy to put up a PR that accomplishes this – I recently did something similar in another view layout library.