Function pointers aren't bad/need protocols/need contracts

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
Interactive_Matter
XCore Addict
Posts: 216
Joined: Wed Feb 10, 2010 10:26 am

Function pointers aren't bad/need protocols/need contracts

Post by Interactive_Matter »

After finishing my first real world project in XC I noticed that omitting pointers in XC is not such a bad idea. At the beginning it feels akward but in the end you can work with it well enough.
But omitting function pointers is a bit too hard.

What are function pointers used for (acc. to my opinion):
Function pointers provide some kind of contract. They specify that a certain (unkown at compile time) function will be called with a specific signature.
It can be used to specifiy how a function should look like to be used. E.g. for callbacks or as part of structs to specify some kind of interface.

But XMOS got channels and soon (hopefully) protocols!
True. The protocol on a channel (if it is checked by the compiler or not) is exactly that kind of interface. More so. int main program the whole solution is assembled form different parts (Inversion of Control).
This is quite great and exactly what I want?
Not quite. Channels take up a lot of resources and are builtin hardware. They are great for communication between threads. But they are a bit too expensive to use as design pattern. Often you need some kind of plugability between components without the need for separate threads and channels (e.g. in th SD card solution there is no need to use threads that separate between the file system and the access layer - physical access can be usefull in a separate thread for prefetching).

Uhhm and what is the point?
There is a need to specify some kind of interface. E.g. the FAT16 feilsystem can work with anything that looks like a file system. So function pointers in C often provide that kind of interface. Hard to say if it is neccessary to support function pointers in XC. Since in XC we got threads and protocols (lets assume we already got them). If you already got protocols between threads (whishfull thinking) why not use them also in the same thread to pass information between modules. By that threads just become special modules.

What do you think?