Anchorage icon indicating copy to clipboard operation
Anchorage copied to clipboard

Priority documentation not matching reality

Open indieSoftware opened this issue 6 years ago • 4 comments

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.

indieSoftware avatar Mar 29 '18 18:03 indieSoftware

@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.

ZevEisenberg avatar Mar 29 '18 19:03 ZevEisenberg

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

view.topAnchor == container.topAnchor ~ 1

jvisenti avatar Mar 31 '18 18:03 jvisenti

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.

ZevEisenberg avatar Apr 02 '18 15:04 ZevEisenberg

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))
}

indieSoftware avatar Apr 18 '18 08:04 indieSoftware