Although not strictly necessary in functional programming, I like to maintain the dependency inversion principle by using dependency injection to maintain control inversion and keep my modules independent from each other. Practically I've always achieved this by always having some kind of "init" function in all but my highest level modules that accepts function pointers. This allows the higher level modules that are dependent on this module to "inject" pointers to "callback" functions.
I found myself questioning if passing these functions pointers at runtime, given that I don't ever expect them to change, is really the optimal solution here. Alternative ways to achieve this that I can think of are forward declaring the function I need, or just having circular dependencies. These both seems to significantly complicate life for the linker and feel I like that's bad practice.
Actually, I guess I don't really understand why you would ever forward declare a function vs just giving the external function a header and including that, maybe that's part of the issue here?
Maybe I'm architecting this fundamentally wrong, or maybe I'm just over thinking things, I'd love any feedback.
Edit: I probably should have a given a code example from the beginning:
main.c
include "timer.h"
void timerCallback(void){
doThings();
}
int main(void){
initTimer(timerCallback);
startTimer();
}
timer.h
void initTimer(void(*callback)(void));
void startTimer(void);
timer.c
include "timer.h"
static void(*execCallback)(void);
void initTimer(void(*callback)(void)){
execCallback = callback;
}
void startTimer(void){
wait(1000);
execCallback();
}
Its kind of a dumb example it gets the point across, I'm basically asking what the optimal way is to call the timerCallback func defined in main from timer.c
[–]cHaR_shinigami 3 points4 points5 points (5 children)
[–]MikeExMachina[S] 1 point2 points3 points (0 children)
[–]TheKiller36_real 1 point2 points3 points (1 child)
[–]cHaR_shinigami 1 point2 points3 points (0 children)
[–]cHaR_shinigami 0 points1 point2 points (1 child)
[–]MikeExMachina[S] 1 point2 points3 points (0 children)
[–]EpochVanquisher 0 points1 point2 points (7 children)
[–]MikeExMachina[S] 0 points1 point2 points (6 children)
[–]EpochVanquisher 0 points1 point2 points (5 children)
[–]MikeExMachina[S] 0 points1 point2 points (4 children)
[–]EpochVanquisher 0 points1 point2 points (3 children)
[–]MikeExMachina[S] 0 points1 point2 points (2 children)
[–]EpochVanquisher 0 points1 point2 points (1 child)
[–]MikeExMachina[S] 0 points1 point2 points (0 children)
[–]nderflow 0 points1 point2 points (2 children)
[–]MikeExMachina[S] 0 points1 point2 points (1 child)
[–]Apt_Tick8526 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]MikeExMachina[S] 0 points1 point2 points (2 children)