robb / Cartography

A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:
Other
7.35k stars 524 forks source link

Add compile-time safety for Edges #351

Open jayrhynas opened 1 year ago

jayrhynas commented 1 year ago

Splits Edge type into EdgeXAxis and EdgeYAxis types Provides compile-time safety to ensure that invalid constraints are not created between edges on different axes.

Before this change, the following code would compile but raise an exception at runtime:

constrain(view1, view2) { view1, view2 in
  view1.top == view2.left
}
Uncaught Exception at File.swift:2: Constraint improperly relates anchors of incompatible types: <NSLayoutConstraint:0x600002e9ccd0 UIView:0x133d0f610.top == UIView:0x133d0f090.left   (active)> (NSInvalidLayoutConstraintException)

Now, this will raise a compile time error:

Binary operator '==' cannot be applied to operands of type 'EdgeYAxis' and 'EdgeXAxis'
jayrhynas commented 1 year ago

This should be merged after #352 if both are going to be merged. jayrhynas/Cartography:de0452d updates the offset operator to compile with this PR.