Strange glitch with unary operators

Hi,


this is my code:


var a = 5
var b = 8
var e = 12
var d = 33
var k = 100

-a + -b - -e + -d


The console displays an error message in the 7th line


Playground execution failed: error: MyPlayground 5.playground:4:14: error: expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

-a + -b - -e + -d

~~~~~~~~~^~~~


Why?

Replies

Reason is the message : expression is too complex !


In fact, parsing requires too many items to be built, compiler has to give up.

In this expression there are only 4 variables, only 4 arithmetic operators and only 4 unary operators.


Is it really a complex expression?


Below is similar expression, it differs only in the last arithmetic operator. It is executed normally.


-a + -b - -e - -d


And next line is more complicated, it has 5 variables. But it is executed normally too.


-a - -b - -e - -d - -k


Why is this happening?

Is it really a complex expression?

Yes. The problem here is that the compiler is doing a lot of type inference, which is essentially a search through a large space of operator overloads that may or may not resolve to a valid expression. If the search space is too big, you get this error.

The compiler should be able to do a better of this and things continue to improve in this area. However, from your perspective, as a user of the compiler, you just have to accept this and simplify any expressions that trigger the problem. There’s two ways to do this:

  • Split the expression into two

  • As type information (for example, you can add

    as Int
    to one of the subexpressions)

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

This link has a some collection of simple expressions that turn out to be complex. This is a real glitch swift


https://forums.developer.apple.com/thread/19463