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
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:
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.
Paralayout's
applyVerticalSubviewDistribution
andapplyHorizontalSubviewDistribution
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:
to:
But that's just the beginning. Since result builders allow for inline
if
statements, they'd enable writing distribution like: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:
@resultbuilder public struct ViewDistributionBuilder
type.applyVerticalSubviewDistribution
method that takes a@ViewDistributionBuilder _ distribution: () -> [ViewDistributionSpecifying]
closure rather than a_ distribution: [ViewDistributionSpecifying]
argument. This new method would call through to the existingapplyVerticalSubviewDistribution
method.applyHorizontalSubviewDistribution
method that takes a@ViewDistributionBuilder _ distribution: () -> [ViewDistributionSpecifying]
closure rather than a_ distribution: [ViewDistributionSpecifying]
argument. This new method would call through to the existingapplyVerticalSubviewDistribution
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.
The text was updated successfully, but these errors were encountered: