Why abstract forwarding is needed?

Why we need explicitly declare that we want to @:forward the base type functionality to the abstract?

  1. Isn’t the haxe compiler can figure it out by itself that we use X base-type function?
  2. Why not just always forward all functionality to the abstract type, anyway abstracts not exist in the output

You might not always want to have the methods of the underlying type available. For instance, you can use this to implement a read-only / immutable array, by forwarding only those methods that don’t modify it:

Another reason might be that you want to implement a method with the same name as an already existing one on the underlying type, but use a different signature for it. Since Haxe doesn’t have overloading, “auto-forwarding” like you propose would lead to a clash.

2 Likes