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.

2018-05-14 01:36:27
To be honest I see 2 use-cases for dynamic list:
  • Fast prototyping: but a full blown dynamic language is probably better suited for that.
  • Heterogeneous collections: when you need heterogeneous collections, you have usually have a very specific need (collection of Animals: dog, cat, etc ...) with very specific methods (eat, sleep, hunt) that a generic collection does not cover so it's written from scratch anyway.

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

2018-05-14 09:11:15
Looks like a reimplementation of json.nim to me.
2018-05-14 15:18:23