Comments (5)
Pattern matching could be used to prevent orders that aren't supported or the math could be inversed within the Anchorage operator to achieve the desired effect using auto layout's native evaluation order.
from anchorage.
This stems from the fact that the operators are an interface into building a LayoutExpression
, which contains a constant and a multiplier, and these are always assumed to be of the form:
y = multiplier * x + constant
In the sample that @BrianSemiglia posted, the original expression looked like this:
y = (x + constant) * multiplier
The user of this code would probably expect that the multiplier would distribute across the expression, resulting in this:
y = x * multiplier + constant * multiplier
However, order of operations is not honored. No matter the order, the constant and multiplier are both just set on the expression directly.
What should we do about it?
We could fix this by introducing more intermediate types to capture the order of operations, but that would be a breaking change: anyone who currently has some code that uses the current behavior is technically using it wrong, but if their code is working for them, correcting the multiplicative distribution would change their layout.
Instead, I think it might be a good idea to build those intermediate types to capture the order of operations, but then use availability/deprecation annotations to show the user a warning that they are using Anchorage incorrectly (i.e. "holding it wrong"). In my opinion, an Anchorage expression should look like the underlying expression it represents, which always takes the form y = mx + b
.
from anchorage.
I agree with @ZevEisenberg that the expression format should be enforced. I don't think there's a strong use case for the distributive property when building layout constraints, except in contrived examples.
Perhaps adding a deprecation/warning to the *
and /
overloads that take a LayoutExpression
on the LHS could help? However, that would preclude valid expressions like
view2.widthAnchor == 50 + view1.widthAnchor / 2
from anchorage.
We could be strict about the ordering as well: anchor * multiplier + constant
. Not sure if people are using other styles much. I'd be happy to run a patched branch against our internal projects and see if it turns up any issues.
Good call on the overrides. We may not need any new intermediate types; just stricter annotations on existing ones.
Edit: math is hard
from anchorage.
To clarify: I think it makes sense to accept any expressions that are unambiguously, algebraically equivalent to y = mx + b
, and reject any that would be different if you apply distribution.
from anchorage.
Related Issues (20)
- Prevent duplicate constraints. HOT 5
- Manual installation instructions HOT 2
- Relative alignment samples outdated HOT 2
- View could not display in full-screen size, black gap on top and also at bottom HOT 2
- Priority documentation not matching reality HOT 4
- Use of `BinaryFloatingPoint` types with custom operators. HOT 7
- Constructing an `AnchorPair` outside of Anchorage will be impossible HOT 4
- Multi-binding syntax
- EXC_BAD_ACCESS switching from 4.0.0 to 4.1.0+ HOT 4
- Carthage prebuilt framework
- Update for Swift 5.0 HOT 4
- Add a shorthand for accessibilityIdentifier HOT 1
- Mark invalid combinations of anchors as deprecated HOT 1
- First time anchorage-usage confusing HOT 1
- Remove convenience accessors on UIViewController subclasses HOT 2
- Type-checking Performance HOT 10
- Add a logo
- Please Update for Swift 5.2 HOT 2
- Carthage problems on Xcode 12 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anchorage.