Scala – initialization order of vals

Vals are initialized in the order they are declared (well, precisely, non-lazy vals are), so properties is getting initialized before loadedProps. Or in other words, loadedProps is still null when propertiesis getting initialized. The simplest solution here is to define loadedProps before properties: class Config { private val loadedProps = { val p = new … Read more

Fun with uninitialized variables and compiler (GCC)

I’m just curious to know why this sudden change in the behavior of uninitialized bool? Disassemble the code and see what the compiler’s doing. My guess: since the value is now only used locally, the compiler optimizes it away completely. Since the behaviour is undefined anyway, the compiler can safely just assume any value, e.g. … Read more

How to initialize a struct to 0 in C++

Before we start: Let me point out that a lot of the confusion around this syntax comes because in C and C++ you can use the = {0} syntax to initialize all members of a C-style array to zero! See here: https://en.cppreference.com/w/c/language/array_initialization. This works: // z has type int[3] and holds all zeroes, as: `{0, … Read more

Initialize field before super constructor runs?

No, there is no way to do this. According to the language specs, instance variables aren’t even initialized until a super() call has been made. These are the steps performed during the constructor step of class instance creation, taken from the link: Assign the arguments for the constructor to newly created parameter variables for this … Read more