Giter VIP home page Giter VIP logo

Comments (5)

BrianSemiglia avatar BrianSemiglia commented on May 14, 2024

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.

ZevEisenberg avatar ZevEisenberg commented on May 14, 2024

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.

jvisenti avatar jvisenti commented on May 14, 2024

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.

ZevEisenberg avatar ZevEisenberg commented on May 14, 2024

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.

ZevEisenberg avatar ZevEisenberg commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.