Running down the rabbit hole of my earlier questions. I've decided to make the equivalent to a dynamic list as you would see in an interpreted language.
Would such a module be of interest to the public? If not, I'll write a nice limited version for myself.
Essentially, the following would be possible:
import dynamiclists
generateDynamicList("MM", int, float, string)
var x = MM()
x.add(3.14)
x.add(3)
x.add(5)
x.add("hello")
for a in x:
echo type(a), ", ", $a
# outputs:
# float, 3.14
# int, 3
# int, 5
# string, hello
Or, more usefully in my case:
import dynamiclists
# NOTE: the following two objects are NOT related. They merely
# have the same procedure.
type
Zippy = ref object of RootObj
a: int
DD = ref object of RootObj
b: string
proc say_hi(self: Zippy): void =
echo "ding"
proc say_hi(self: DD): void =
echo "dong"
var z = Zippy()
var doodah = DD()
generateDynamicList("NN", Zippy, DD)
attachProcToDynamicList("NN", "say_hi")
var y = NN()
y.add(z)
y.add(doodah)
y[0].say_hi()
y[1].say_hi()
# outputs:
# ding
# dong
I no interest, I'll keep it to myself as the level of meta is way high; and there are a lot of border cases to track.
What is often requested is a way to generate a common interface for various types, it is being work on using Nim concepts, and here is a current implementation using macros: https://github.com/andreaferretti/interfaced