As shown in the example from the previous section, this
in abstract methods refers to the underlying data. For AbstractInt
methods, this
is therefore a variable of type Int
.
abstract AbstractInt(Int) { inline public function new(i:Int) { this = i; } }
The abstract
keyword can be used in abstract methods to refer to the current instance as an abstract type, rather than referring to the underlying data with the this
keyword.
This can be useful when an abstract method needs to call other methods which accept an argument of the abstract type. For example, suppose class Main
defines a method takeAbstractInt
:
static public function takeAbstractInt(x:AbstractInt) { // ... }
To call takeAbstractInt
from within an AbstractInt
method, we must use the abstract
keyword:
inline public function test() { Main.takeAbstractInt(abstract); }
If we instead wrote Main.takeAbstractInt(this)
, this would be a type error, because takeAbstractInt
does not accept an argument of type Int
.