[swift-users] cast operator precedence

Jens Alfke jens at mooseyard.com
Fri Dec 4 13:30:19 CST 2015


> On Dec 4, 2015, at 3:21 AM, Elena Vilchik <vilchik.elena at gmail.com> wrote:
> 
> I've found here <https://developer.apple.com/library/prerelease/ios/documentation/Swift/Reference/Swift_StandardLibrary_Operators/index.html#//apple_ref/doc/uid/TP40016054> that "as" operator has a lower precedence than "*" operator (132 vs 160).
> Why then this code compiles if multiplication should be made first? 
> 
> var x = 1
> print(x as Int * 100)

“as ___” is a *postfix* operator (the following type name has to be considered part of the ‘as’.) So there’s only one way to parse that expression as valid Swift, and thus the parser doesn’t even have to look at operator precedence.

In other words, `x as (Int * 100)` isn’t valid Swift syntax, so the parser doesn’t even consider it.

—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20151204/42b23b4c/attachment.html>


More information about the swift-users mailing list