Rightpoint / Anchorage

A collection of operators and utilities that simplify iOS layout code.
MIT License
627 stars 46 forks source link

Priority documentation not matching reality #55

Open indieSoftware opened 6 years ago

indieSoftware commented 6 years ago

According to the readme the priority can be set with a number like

view.centerXAnchor == view.superview.centerXAnchor + 20 ~ 752

but that doesn't seem to work. The compiler always complains about it with the message Cannot convert value of type 'Int' to expected argument type 'Priority'.

However, using an instance of Priority works:

view.centerXAnchor == view.superview.centerXAnchor + 20 ~ .init(752)

So either the documentation is wrong or there is a bug which doesn't allow to use an Integer directly.

ZevEisenberg commented 6 years ago

@indieSoftware good catch. This appears to be a documentation bug, although it would be interesting to see if we could support the usage as it is spelled in the docs. However, I think it is probably more idiomatic to use the .high + 2 spelling instead of the literal 752.

jvisenti commented 6 years ago

Looks like there's a priority issue with the ~ operator. This compiles fine:

view.topAnchor == container.topAnchor ~ 1
ZevEisenberg commented 6 years ago

I don't think it's a priority issue. The priorities on ~ seem reasonable. The given code should be hitting this function:

@discardableResult public func ~ <T, U>(lhs: LayoutExpression<T, U>, rhs: Priority) -> LayoutExpression<T, U> {
    var expr = lhs
    expr.priority = rhs
    return expr
}

So the 752 should be going through Priority's ExpressibleByIntegerLiteral conformance. The fact that it's not is what's confusing to me, since the compiler clearly understands that the type of the right-hand side should be a Priority, and it has a value that it should be able to convert.

indieSoftware commented 6 years ago

The following doesn't work either (is not mentioned by the documentation, but would be nice to have):

button.widthAnchor >= 44 ~ 990

I have to use it this way:

button.widthAnchor >= 44 ~ Priority(990)

or

button.widthAnchor >= 44 ~ .required - 10

@ZevEisenberg is right, for some reason the ExpressibleByIntegerLiteral doesn't trigger when relied on as operator parameter. When I add the following statement to bypass it and specifying the type explicitly, then button.widthAnchor >= 44 ~ 990 works:

@discardableResult public func ~ (lhs: Int, rhs: Int) -> LayoutExpression<NSLayoutDimension, CGFloat> {
    return LayoutExpression(constant: CGFloat(lhs), priority: Priority(rhs))
}