How do I avoid overwriting member variables initialized in a superclass in Scala with an underscore? - scala

My superclass initializes member variables through reflection via annotation-based injection. How do I prevent a subclass from overriding the value of a member variable set by the superclass? The problem is that the primary constructor of the superclass initializes the subclass's member variable and then the subclass's primary constructor (which is called subsequently) writes over that value when the member variable's declaration is executed.

The answer I discovered is to initialize the subclass's member variable with an underscore. This is actually explained quite nicely in the language specification in section 4.2 which talks about giving variable's initialized with an underscore a getter function that returns its current value.

Related

How to keep default constructor after declared another in java?

I want to access default constructor from a class, (after I create another constructors with any parameters), without declared him!
How can I access default constructor without declared him?

Why Dart can't define an instance property that depends on another instance property [duplicate]

This question already has answers here:
Difference between assigning the values in parameter list and initializer list
(2 answers)
Closed 2 years ago.
Flutter: The instance member'stars' can't be accessed in an initializer. Error
I asked the above question and received the following answer as the reason for the error.
reason : in dart you can't create a class level variable depends with
another variable
↑ Is there any explanation in the official Dart, Flutter document (or something similar)?
Or is it derived from the Dart constructor mechanism?
If so, I would like to understand how the constructor works, which is the reason for this error.
Is there any relevant part in the official Dart, Flutter documentation, etc.?
It is in fact due to Dart's constructor mechanism, specifically the order of execution during construction, as well as how Dart references instance variables within an object.
From the Instance Variables section of the Dart Language Tour:
If you initialize an instance variable where it is declared (instead of in a constructor or method), the value is set when the instance is created, which is before the constructor and its initializer list execute.
When you reference an instance variable in a Dart class via variableName there is an implied this reference to the instantiated object containing the variable. Due to the order of execution of Dart's object construction, this cannot reference the object during instance variable declaration because the object has not been created yet. Therefore, this.variableName and thus variableName (with implied this) cannot actually reference the instance variable.

Swift 4.2 - __shared attribute near type

https://developer.apple.com/documentation/swift/double?changes=latest_minor
In updated version of class definition I found init method for Double with NSNumber near type the unknown keyword __shared. What does it mean?
The __shared parameter annotation means that a value type parameter can be passed by reference.
For value types, this enables us to elide a copy before we make the call and instead pass a reference pointing right at the memory we allocated. SIL calls this convention in_guaranteed for (indirect reference with guaranteed lifetime). It's currently the way we pass self in non-mutating functions.
It was introduced by the Ownership Manifesto.
Here is the difference between the ownership annotations:
inout: mutating pointer-like value
__shared: non-mutating pointer-like value
__owned: an explicit way of writing the default
Here is a summary of the manifesto: Swift Ownership Manifesto TL;DR.

What is the motivation for requiring "override" on an initialiser that has the same signature as one in a base?

For methods it is clear to me that accidentally overriding a method could have bad consequences, so requiring developers to be explicit about it with the "override" keyword seems like a good idea.
However, as initialisers are invoked on a type (as it were) instead of on an object, I don't understand what overriding means in this context nor what kind of mistakes the requirement for the "override" keyword on initialisers is preventing.
I find the override modifier to provide only a convenience and safety function of expressing the intent of a developer to either override an existing member of a class (when the modifier is present) or to introduce a new one (when the modifier is missing) so the compiler can perform the corresponding compile-time checks and inform the developer if the expressed intent could not be implemented (because there is nothing to override or because there is a signature collision with a member defined in one of the base classes).
You can argue that there is a difference between overriding a virtual member or providing a distinct statically dispatched initializer replicating the signature of an initializer of the base class. Technically, yes; but not semantically. Semantically you may still want to replicate the signature of an initializer of the base class, and the compiler is ready to perform the corresponding compile-time signature checks.
The Swift Language Guide in the chapter "Initialization" under the title Initializer Inheritance and Overriding says:
When you write a subclass initializer that matches a superclass designated initializer, you are effectively providing an override of that designated initializer. Therefore, you must write the override modifier before the subclass’s initializer definition. ...
As with an overridden property, method or subscript, the presence of the override modifier prompts Swift to check that the superclass has a matching designated initializer to be overridden, and validates that the parameters for your overriding initializer have been specified as intended.
From this excerpt the role of the override modifier indeed appears to be small — to verify the intent of a developer to match a signature from the base class or not.

Using self in a callback passed to super.init

I've got a base class. In this base class I take some callbacks in the init method. In these callbacks I'd like to reference the derived class's self. However, Swift complains that the lambdas reference self before super.init. Of course, I know that in reality, the base class doesn't invoke the callbacks actually inside super.init and even if it did, that's not clearly illegal, since it would not be before super.init.
How can I pass a callback to super.init that references self?
It is not possible to reference self in anyway before you have called the super.init. self can only be referenced when the initialisation is done. You have to look for alternative ways to create your object (factory methods, builder pattern).
I managed to work around this meaningless restriction by refactoring the class somewhat. Instead of simply passing the constructor the data it needs directly, instead there's a getter for the data which just happens to be called exactly once by the superclass constructor. The variable that stores this data, instead of being immutable and proper, now is initialized with nil (i.e. left uninitialized) and then initialized later through the getter and then there's another getter-only computed property.
So now anyone who tries to read the class will be thoroughly confused by the worthless meandering around the point, but it does actually have the desired semantics.