On 2010-05-26 15:44:58 -0400, Jacob Carlborg <d...@me.com> said:

On 2010-05-26 16.20, Michel Fortin wrote:

In D you can't do that currently, but here's how it could be added.
First add an optional attribute for interface members:

interface TableDelegate {
...
@optional NSColor backgroundColorForRow(NSTableView table, size_t row);
}

Classes implementing the interface can omit the @optional methods. When
omitted, the interface function is null. When using the interface, you
must check first if the optional function is implemented by making sure
the address isn't null:

TableDelegate delegate = this.delegate;

if (&delegate.backgroundColorForRow != null) {
NSColor color = delegate.backgroundColorForRow(this, row);
...draw background color...
}

This would work somewhat similarily to weak-linked symbols.

That is quite a clever idea, to check for null. The question is then how much trouble would that be to implement.

If I had to guess those things would need to be done:

1. Add the "@optional" attribute token to the lexer
2. Allow @optional to be attached to functions in an interface
3. When checking if a class correctly implements an interface, allow unimplemented @optional functions 4. When generating the interface's vtable, use null for unimplemented @optional functions

I think that's all. Checking for null is easily done by retrieving the delegate pointer (which you can already do), and calling the function uses the usual calling mechanism for interfaces. I expect that trying to call an unimplemented interface function would be like trying to call a null function pointer.

--
Michel Fortin
michel.for...@michelf.com
http://michelf.com/

Reply via email to