Esqarrouth
Esqarrouth

Reputation: 39181

How does Swift type inference affect compile and run time?

var number = 3

vs

var number: Int = 3

How does using specific types vs type inference affect compile time? Has anyone done experiments or some math on this topic?

Does this runtime affect at all in anyway?

Upvotes: 1

Views: 367

Answers (2)

gnasher729
gnasher729

Reputation: 52530

Compile time: In most cases, this will be trivial. In your example, 3 is an integer literal; integer literals can adapt to their use, but it's trivial that number will have type Int.

At runtime, there is absolutely no difference. Both statements are 100 percent equivalent.

Upvotes: 5

Björn Ro
Björn Ro

Reputation: 780

Both examples will do the same. differences appear when using float values.

var double = 2.5
var float : Float = 2.5

In swift its better to write less code. This makes the code healthier and for sure it will be faster.

Upvotes: 0

Related Questions