ammer

Instance methods

Although C does not have the concept of instance methods (unlike, for example, Java or C++), native libraries often define APIs which simulate such a feature by passing a pointer to a struct or opaque type as the first argument.

In ammer, it is possible to use such methods as instance methods rather than static methods, resulting in more readable client code. To achieve this, a function must be declared in an opaque type definition or a struct definition with two requirements:

When calling instance methods declared this way, the This argument is omitted, as it is automatically filled in.

Example: instance method

class FoobarStruct extends ammer.def.Struct<"struct foobar_s", Foobar> {
  public function do_something(_:ammer.ffi.This, a:Int):Int;
}

In this example, do_something is an instance method. In Haxe code, it could be used like this:

var x:FoobarStruct = #dummy expr/*...*/;
x.do_something(42);

Note that the first argument for do_something is not specified – x is used automatically.

« Previous: Structs Next: Enums »