0

I'm facing an issue with optionals and need a deeper understanding of the below scenario.

Here is the code,

class X {
    let b: Int! //not working
    let c: Int? //not working

    var q: Int! //working
    var r: Int? //working
}

let x = X()
x.a

In the above code, I've simply created 4 combinations of let and var along with optional and implicitly unwrapped optional.

The above code gives me the below compiler error:

Return from initializer without initializing all stored properties

Issue:

Why is it necessary to assign values of b and c in init if they both have nil as the default values? And if it is required, why is it not mandatory to assign q and r in init?.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • 4
    Since variable b and c are marked as let which means their value cannot be change so they expecting integer value while in case of q and r compiler assume that variable may contain value in future. – Nirbhay Singh Nov 11 '19 at 07:18
  • @NirbhaySingh But for optionals, we do have a default `nil` value right? – PGDev Nov 11 '19 at 07:19
  • @PGDev Specifying optional don't mean that by default they will be nil; Optional is only a way of telling compiler that the value may be nil and compiler needs to check the value of variable before using it. Refer the following link for optionals - [Optionals-explained-simply](https://hackernoon.com/swift-optionals-explained-simply-e109a4297298) – udbhateja Nov 11 '19 at 07:24
  • @bhatejaud If this is the case, then `q` and `r` shouldn't work. Because no value means it is necessary to give a value in `init`. – PGDev Nov 11 '19 at 07:26
  • Constants should be initialized on the init constructor. – Kumar KL Nov 11 '19 at 07:26
  • @KumarKL That's not true. If we give them a default value, they don't need be assigned in `init`. – PGDev Nov 11 '19 at 07:27
  • @PGDev What you are asking has nothing to do about optionals, it's about let and var. var means variable and they don't need any value during declaration but let means constant and their declaration + initialisation happens at same time – udbhateja Nov 11 '19 at 07:27
  • @bhatejaud In that case only `var a: Int` should work. But it doesn't. So, it definitely has to do with the `optionals`. – PGDev Nov 11 '19 at 07:28
  • @PGDev, It's not just about default value, we can use diff initializers where we can pass the values and constants has to be initialized on the init. – Kumar KL Nov 11 '19 at 07:30
  • @PGDev Try using `let b: Int! = nil` `let c: Int? =nil` It will work – udbhateja Nov 11 '19 at 07:30
  • @PGDev From what I gather, the compiler is designed to force all constants to be given an explicit assignment. Hence you either do `let b: Int! = nil` or put `b = nil` in the `init`. It's the way compiler deals with `let`. – staticVoidMan Nov 11 '19 at 07:30
  • @bhatejaud That's what is the question. Why do I need to assign it in `let` if same is working for `var`. Compiler asks for the value, be it default or in `init`. – PGDev Nov 11 '19 at 07:31
  • let keyword is used to declare a constant and var keyword is used to declare a variable. Variables created with both of them are either references/pointers or values. The difference between them is that when you create a constant with let you have to give it a value upon declaration (or within the calling scope)1 and you can't reassign it. And when you declare a variable with var it can either be assigned right away or at a later time or not at all (i.e. be nil). – Dhaval Raval Nov 11 '19 at 07:37
  • nil isn't a value, setting something to nil means it has no value. – Joakim Danielson Nov 11 '19 at 07:39
  • @JoakimDanielson Shouldn't it work for both `let` and `var` then? Means we should be forced to assign the value for both in `init`? – PGDev Nov 11 '19 at 07:42
  • Your all stored property need a value or you can use optional values for them – Dhaval Raval Nov 11 '19 at 07:44
  • For me it is a contradiction between `let` and optional that expians it, `let` means the variable will be assigned a value directly (at latest in the init method) while optional means the variable can be set at _any_ time or even not at all. – Joakim Danielson Nov 11 '19 at 08:00

0 Answers0