Let declarations require an initializer expression

I read "Swift Language" under "Simple Values"

"Use let to make a constant and var to make a variable. The value of a constant should not be known at compile time, but you must assign this value exactly once"

So, I think I can do it.

let aConstant:Int aConstant = 5 

But I get let the declarations require an initializer expression !!

Why? What do they mean by “Constant value does not need to be known at compile time”?

+6
source share
4 answers

From the Swift Language Reference:

When a constant is declared in the global scope, it must be initialized with a value.

You can only delay initialization of the constant in classes / structures, where you can initialize it in the class / structure initializer.

The value "Constant value does not have to be known at compile time" refers to the constant value. In C / Objective-C, a global constant must be assigned a value that can be calculated by the compiler (usually literally, like 10 or @"Hello" ). Objective-C does not allow:

 static const int foo = 10; // OK static const int bar = calculate_bar(); // Error: Initializer element is not a compile-time constant 

In Swift, you do not have this limitation:

 let foo = 10 // OK let bar = calculateBar(); // OK 

Edit:

The following statement in the original answer is incorrect:

You can only delay initialization of the constant in classes / structures, where you can initialize it in the class / structure initializer.

The only place you cannot set aside is on a global scale (i.e. higher levels of let ). Although it is true that you can defer initialization in a class / structure, this is not the only place. It is also legal, for example:

 func foo() { let bar: Int bar = 1 } 
+11
source

The constant is not required to be known at compilation, but it should have value after initialization:

 class MyClass: NSObject { let aConstant: Integer; // no value init() { aConstant = 4; // must have a value before calling super super.init(); } } 

This allows you to set a constant to a value after it is declared and is potentially unknown at compile time.

+2
source

The let keyword defines a constant by definition.

Therefore, you cannot change it after installing it.

Since this is the case, they need to be initialized when they are declared!

The solution here is as follows:

 let aConstant = 5 

or change it to var

 var aNonConstant:Int aNonConstant = 5 
+1
source

Answer for Swift 2:

You can write constants as follows:

 let aConstant:Int aConstant = 5 

Setting the type in this way means: "It will be permanent, and it will make a difference when you need it." Please note that you cannot use a constant before its value, there is a compile-time error:

The 'aConstant' constant is used before initialization

In addition, you can set aConstant only once. If you try to set the value a second time, a compile-time error will occur:

The immutable value of 'aConstant' can only be initialized once.

In any case, you cannot do this for global constants, there is a compile-time error:

'Let' global declaration requires initialization expression

+1
source

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


All Articles