HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[code.haxe.org] Other - Adding static methods to existing classes #25

Open utterances-bot opened 5 years ago

utterances-bot commented 5 years ago

Adding static methods to existing classes - Other - Haxe programming language cookbook

Haxe allows you to add static methods to existing classes (eg. Math) via the static extensions feature. The secret sauce is to specify the first parameter of the extension as Class where X is the class you want to add static methods to (eg. Math), and to make the method public.

https://code.haxe.org/category/other/adding-static-methods-to-existing-classes.html

geranim0 commented 5 years ago

very nice with Borat accent

migmit commented 2 years ago

Doesn't seem to work with @:using though.

migmit commented 2 years ago

To elaborate: yes, adding static methods with @:using seems pointless, but it helps with enums:

@:using(MyEnum.MyEnumExt) enum MyEnum {A; B;}
class MyEnumExt {
  public static function default(_: Enum<MyEnum>): MyEnum return A;
}

It works with using MyEnum.MyEnumExt, but not with metadata.