I would like to use Self in init parameters like so:
class A {
public init(finishBlock: ((_ operation: Self) -> Void)? = nil) {...}
}
I know I could use "A" in this place, but I would like to achieve that if some class inherits from A, then it's initializer would know operation as it's class type and not as just A. So for example if I wrote:
class B: A {
public init(finishBlock: ((_ operation: Self) -> Void)? = nil) {...}
public func fooOnlyInB() {}
}
I could then use:
let b = B { (operation) in
operation.fooOnlyInB()
}
Is this somehow possible?
The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.
The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
Technically both self and this are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self explicitly as first parameter to an instance method in Python, whereas this is not the case with Java.
Instead of using Self
or A
in each of the initialisers, you can simply override each subclass' initialiser to use its own type as operation
.
This works because A
's initialiser states that operation
should be a type that conforms to A
, and when you override it you have the liberty to use a subclass of A
as operation
instead. However, if you change operation
to an unrelated type such as String
or Int
, the compiler will not override the existing initialiser.
Firstly, define A
with its init
:
class A {
init(finishBlock: ((_ operation: A) -> Void)?) {...}
}
Now to create a subclass, you must override init
using the subclass' type as operation
instead. In your call to super.init
, force upcast operation
($0
) to your subclass' type, and call finishBlock
with this casted operation
.
class B: A {
override init(finishBlock: ((_ operation: B) -> Void)?) {
// Perform custom initialisation...
super.init { finishBlock?($0 as! B) }
}
func fooOnlyInB() {
print("foo")
}
}
B
's initialiser now passes B
as operation
, which means that you don't need to cast it yourself anymore! This is thanks to the fact that you can override an init
with a more specific type, in this case B
.
let b = B { operation in
operation.fooOnlyInB() // prints "foo"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With