Metacode mechanics

Fredrik Olsson peylow at treyst.se
Tue Mar 28 22:48:36 PST 2006


Andrew Fedoniouk skrev:
>> I definitely agree with the array literals.  And the IFTI.  I guess I 
>> still
>> think that reflection is an important piece that should happen, but the 
>> other
>> pieces are probably much more pertinent to the language in the end and as 
>> such,
>> should be handled first.
>>
> 
> Why do you think that reflection (I suppose you mean runtime reflection) is 
> so good?
> Do you have any practical examples where it is so needed?
> 
> Andrew. 
> 
> 
If done correctly you can pretty much base an API on it, and a really 
good one. Lets take Cocoa (OpenSTEP) as an example. It is written in 
Objective-C, with very good reflections support, and with the 
Smalltalk-OOP inheritance, as opposed to C++/D, etc Simula-OOP 
inheritance that is natural. After all what C++ and D does is just 
syntactic sugar for function pointers in structs :).

The class NSTableView is a 2d grid control, spreadsheet like if you 
will. So how is the table filled with data and managed? With _a single 
object_, called the dataSource. The dataSource object must conform to 
the informal protocol NSTableDataSource. Protocols in Objective-C is 
what we would call interfaces, informal mean that it must not implement 
all of the protocol, just some of it.

Now this is the good part, to make a read-only data view your data 
source needs only implement a few methods; numberOfRowsInTableView:, and 
tableView:objectValueForTableColumn:row: and adding; 
tableView:setObjectValue:forTableColumn:row: would be enough to make it 
writable as well. Making it sortable is yet a single method more.

So how does it work? Simple, when the table view wants to redraw itself, 
it asks the table source if it implements the methods needed, if it 
does, feature on, otherwise, feature off, or even call default handler 
instead (99% of all Cocoa controls have free cut/paste, spelling, etc).

I have written applications using pure win32 API, borlands VCL, Visual 
BAsic 6, .NET, Java swing, and GTK. And for simplicity, consistency and 
redused code size, Cocoa beats them all.

And it is all made possible because of strong support for reflection.

// Fredrik



More information about the Digitalmars-d-dtl mailing list