Extension: Method Calls

Example

struct MyWidget {
  void (*close)(MyWidget*);
}

void my_widget_show(MyWidget*);

MyWidget *w;
w->show();
w->close();

Syntax & Semantics

Suppose the variable w has type struct MyWidget*, and that struct MyWidget contains a function pointer void (*show)(struct MyWidget*). Then the expression w->show() compiles into w->show(w).

If struct MyWidget does not contain a show member, but there is a global function my_widget_show(struct MyWidget*), then the expression w->show() compiles into my_widget_show(w).

See also the inheritance extension.

Implementation

Half-implemented in A compiler: extends compile to look for -> cases that match the global function situation, build new syntax, and call compile recursively on that new syntax.

Cannot use same approach for the function pointer case, unless intentionally ignores w->show(w), but then w->show() and w->show(w) are the same, which could be confusing. Would be nice to be able to have some way to tell itself not to process that new syntax.

Also needs to be able to ask, given a struct type, whether a particular name is a member of that struct, and if so, what is its type.