Fast variable declaration and initialization

Is there a difference between how the following bits of code work?

let x: Int = 4

and

let x: Int
x = 4
+4
source share
2 answers

This:

let x: Int = 4

creates an optional variable xand initializes it to 4. xcan be used without problems.

This:

let x: Int
// Cannot do anything with x yet
x = 4

creates an optional variable xwith no defined value . It cannot be used without first assigning it to a value either directly (as in your example) or the result of any other operator. If you try to use it, you will get a compile time error.

+7
source

, , - , .

.

-1

Source: https://habr.com/ru/post/1658096/


All Articles