var teen: String
myAge > 12 && myAge < 19 ? teen = "You're a Teen" : teen = "You're not a teen"
Upon running my code, I got an error message stating "result of conditional operator '?:' is never mutable.
I tried searching online for a solution and found someone with a similar issue who said he resolved this by placing each result in parentheses, so I did the same and it worked.
var teen: String
myAge > 12 && myAge < 19 ? (teen = "You're a Teen") : (teen = "You're not a teen")
Now when I clicked "run code" it worked just fine. However, I'd like to know WHY this worked. Why do I have to place each result of the condition in parentheses? Please help. Thank you.
It's because of the way the operator precendese rules work out with what you've written. I'll simplify this by replacing myAge > 12 && myAge < 19 with just b. Your code is parsed like so:
(b ? teen = "You're a Teen" : teen) = "You're not a teen"
Which clearly doesn't make sense.
By adding parenthesis, you've forced a particular parsing that actually makes the code valid.
However, there's an even simply, clear way to do this:
There's no need for teen to be mutable
There's no need for teen to be declared and assigned separately
When you're using a conditional operator, it's often nice to group up a complex predicate in parenthesis, but that's a matter of personal style.
Here's how you might improve this:
let teen = (myAge > 12 && myAge < 19) ? "You're a Teen" : "You're not a teen"
I would go further and improve the naming:
let isTeen = (13...19).contains(myAge)
let teenStatusMessage = isTeen ? "You're a Teen" : "You're not a teen"
I can most easily explain it by analogy. Imagine you're writing a math expression like:
1 + 2 * 3
and was wondering why the result was 7 instead of 9. Of course, that's because it evaluates as 1 + (2 * 3), whereas you intended (1 + 2) * 3. Then you might ask: "Why does it parse so strangely?". Precedence are man-made, and one could imagine an alternative convention on mathematical notation where that + takes precedence over *. It would work the way you wanted it to. But one can also imagine another post saying: "Why does it evaluate to 9, I wanted 7!"
Operator precedence rules are kind of like which side of the road a country drives on. It doesn't really matter one way or another, but there needs to be a consistent standard or else chaos ensues.