robb/Cartography

Remove `constrain` closures

Aperta

#145 aperta il 10 lug 2015

 (3 commenti) (0 reazioni) (0 assegnatari)Swift (522 fork)batch import
help wantedquestion

Metriche repository

Star
 (7337 stelle)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

As per @smileyborg in #144:

BTW, on the note of significant breaking changes like this, I'd love to also explore what Cartography would look like if it didn't use closures passed to constraint (or layout) and instead added some "NSLayoutAnchor-like" properties of type LayoutProxy to UIView/NSView, which would let you use the custom operators to create constraints without having to pass the view(s) into a closure first.

I've thought about this, we'd basically replace

constrain(view1, view2) {
    view1.width  == view2.width
    view1.height == view2.height
}

with, if I understood you right, something like

view1.car_width  == view2.car_width
view1.car_height == view2.car_height

There's two aspects I dislike about this approach. One is that I believe the car_ prefix makes it harder to read. I'd like Cartography to be as close to "pseudocode" as possible, and prefix takes away from that.
There may be another way to attach the properties, such as a surrounding function or an operator but I'm worried it takes away clarity.

The other thing is that I like that the constrain function forces users to have their constraints grouped in a single place, and also sections off the part of the code in which Cartography's operator overloading applies.

It also provides a place to attach additional information. E.g. I'd like to add __LINE__ and __FILE__ default arguments to constrain to make it easier to find where a specific constraint was created in code (#97).

Guida contributor