Wow, I didn't think about cyclic imports at first !
I ended up with the approach you mentioned: using a proc type field with a
default value that can be overriden by the end user with a setter.
Finally, messing around with type introspection would have been more
complicated to understand compared to this simple solution.
.
# Define in the library
type
Callback = proc (self: Car)
Car = ref object
speed: int
drive_callback*: Callback
proc new*(car_type: type[Car]) Car =
return Car (drive_callback: default_drive)
proc default_drive(self: Car):
echo "vroom"
proc set_drive_callback*(self: Car, callback: Callback) =
self.drive_callback = callback
# Define by the user
var car = Car()
car.set_drive_callback(
proc (self: Car) =
echo "VROOM !!"
)
Run
PS: Thanks @dawkot for the hint, I might use it for another thing !