Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Compiler performance

I got this statement in Swift code which produces an error when executing in playground:

let colors: [String: [Float]] = ["skyBlue" : [240.0/255.0, 248.0/255.0, 255.0/255.0,1.0], 
"cWhite" : [250.0/255.0, 250.0/255.0, 250.0/255.0, 1.0]]

The error is : expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

Then I changed the arrays element type to Double which just works fine.

However I am asking myself why this happens ? As I said using Double it works just fine. So my guess is that Swift tries to guess the type and therefore Double works better in this example than Float.

like image 562
dehlen Avatar asked Apr 19 '15 17:04

dehlen


1 Answers

Similar issues have been reported before, and (as I understand it) the problem is the automatic type inference for "complicated" expressions. You should file a bug report at Apple.

It compiles with a dictionary of one color, but not with two.

In this concrete case, you can work around it by converting each number in the array to a Float explicitly:

let colors = [
    "skyBlue" : [Float(240.0/255.0), Float(248.0/255.0), Float(255.0/255.0),Float(1.0)], 
    "cWhite" : [Float(250.0/255.0), Float(250.0/255.0), Float(250.0/255.0), Float(1.0)]
]
like image 86
Martin R Avatar answered Oct 10 '22 21:10

Martin R