Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift type-check takes long time

Tags:

xcode

swift

I've added the flag -Xfrontend -warn-long-expression-type-checking=50 to my Swift project to see what is taking too long. I've the following expression:

let s: String = "1234"
let t: Int? = Int(s)

On the Int(s) I get the warning Expression took 52ms to type-check (limit: 50ms). I'm just wondering why this takes more than 50ms to figure out since I specified all types.

like image 966
Haagenti Avatar asked Aug 01 '19 06:08

Haagenti


1 Answers

If we try to run this initializer:

let t: Int? = Int(s, radix: 10)

we can see that the typecheck is a lot faster.

Looking at Int initializers in the code completion, i can see there are a couple that take a String, so my guess is the compiler is just trying to resolve one based on arguments and returning Int? and that takes longer

What we did when we supplied radix was limit the number of possible choices, which is why that code typecheck is faster. That's purely my speculation, of course!

like image 108
zaitsman Avatar answered Nov 07 '22 06:11

zaitsman