Hi, I want to implement `interface` or `dependency injection pattern`. Finally
I want to implement an extensible web framework. For example, I have a Context
object containing a Logger attributes. I just define the interfaces that Logger
should satisfy. Then users can pass their own Logger instance to construct
Context object. My goal is to achieve a loosely coupled application.
However using function pointers directly makes it hard to extend object.
type
Animal* = object
id: int
sleepImpl: proc (a: Animal) {.nimcall, gcsafe.}
barkImpl: proc (a: Animal, b: int, c: int): string {.nimcall, gcsafe.}
danceImpl: proc (a: Animal, b: string): string {.nimcall, gcsafe.}
People* = object
pet: Animal
proc sleep(a: Animal) =
discard
proc bark(a: Animal, b: int, c: int): string =
result = $(a.id + b + c)
proc dance(a: Animal, b: string): string =
result = b
proc newAnimal*(id: int): Animal =
result.id = 1314
result.sleepImpl = sleep
result.barkImpl = bark
result.danceImpl = dance
let people = People(pet: newAnimal(12))
doAssert people.pet.barkImpl(people.pet, 12, 14) == "1340"
Run
If I can't use generics to implement interfaces, I maybe implement something
like `vtables` in C++ or `fat pointer` in Rust(Using macros or so).
Experiment Code:
[https://github.com/xflywind/shene](https://github.com/xflywind/shene)