Hi,
I have implemented DataTable and a decent DSL to make SQL like queries on it (where, select, order, group_by is supported). Table is represented as tuple/object of columns, each column is seq of some type, usually primitive type.
type
DataTable = concept x
x is tuple|object
for f in fields(x):
x is seq ## all columns should be of the same length
The definition is clear and simple, I am pretty happy with it. The names of columns and types of columns are known at compile time, scheme is set in stone at compile time.
Now, I want to define ordered DataTable by one of the columns (think of Table with set primary key). OrderedDataTable is a DataTable, this is the relationship I want to maintain so all procs accepting DataTable should work on OrderedDataTable as well. The key is simply the name of the column the table is sorted on and it is known at compile time. Since key is known at compile time, I though I can have it as type tag only and keep binary representation untouched.
Basically, I was leaning towards something like this
type
OrderedDataTable[key: static[string]] = concept of DataTable
# Not clear what to do with key to preserve it
OrderedDataTable is "type tagged" tuple, its binary representation exactly the same as of untagged tuple, but type info contains extra string tag that I can extract using getTypeInst() in macro or any other means.
Does it sound possible or reasonable?
This is the closest thing I can think of:
import macros
macro dotAccess(o: typed, str: static[string]): untyped =
# Macro to do `o.str`, probably not the best way
newDotExpr(o,newIdentNode(str))
type
DataTable = concept x
x is tuple|object
for f in fields(x):
f is seq # your example had `x` here, wich didn't really make sense to me,
# as if x is tuple or object it can't be seq, no?
OrderedDataTable[key: static[string]] = concept od
od is DataTable
od.dotAccess(key) is seq # Checks that `od` has field `key`
od.k == key # check the key of the concept matches the key of the object
MyDt[k:static[string]] = object
a: seq[int]
b: seq[float]
var ob: MyDt["a"]
echo ob is DataTable # true
echo ob is OrderedDataTable["b"] # false
echo ob is OrderedDataTable["a"] # true
I don't really know how you would access key if it wasn't stored in the object type.
( looking at the generated C, looks like MyDt is a struct with fields a and b, so it should be the same binary representation? I think I remember reading that generics exists only for the nim compiler, they do not affect the C code)