Home » Swift » Limit to number of terms in an expression?

Limit to number of terms in an expression?

Posted by: admin November 30, 2017 Leave a comment

Questions:

Edit: This is confirmed by apple as a compiler error.

The first if expression below (17 terms) compiles, and produces the expected result (false).
The second if expression (18 terms) fails with the error message:

Cannot invoke ‘||’ with an argument list of type ‘($T106,$T110)’.

The two expressions are identical except for the extra term.

I have no problem working around the problem, but I just don’t understand what it is complaining about. Can someone enlighten me as to what stupid mistake I am making? Be gentle, very inexperienced coder here.

import Darwin

var a = -1
if
    a == 0 ||
        a == 1 ||
        a == 2 ||
        a == 3 ||
        a == 4 ||
        a == 5 ||
        a == 6 ||
        a == 7 ||
        a == 8 ||
        a == 9 ||
        a == 10 ||
        a == 11 ||
        a == 12 ||
        a == 13 ||
        a == 14 ||
        a == 15 ||
        a == 16 ||
        a == 17 { println("value was true") } else { println("value was false")}

if
    a == 0 ||
        a == 1 ||
        a == 2 ||
        a == 3 ||
        a == 4 ||
        a == 5 ||
        a == 6 ||
        a == 7 ||
        a == 8 ||
        a == 9 ||
        a == 10 ||
        a == 11 ||
        a == 12 ||
        a == 13 ||
        a == 14 ||
        a == 15 ||
        a == 16 ||
        a == 17 ||
        a == 18 { println("value was true") } else { println("value was false")}
Answers:

While the bug is solved you can use this:

switch a {
case 0...17:
    println("value was true")
default:
    println("value was false")
}