list of anything

spir denis.spir at gmail.com
Sat Dec 18 04:16:30 PST 2010


Hello,

In Lisp-like languages, a list can hold anything:
	(1 "a" (1 "a"))
I do not find it trivial to simulate this in D. Using a linked list or an array: the issue is not with the kind of collection but with elements. In either case, I guess, elements should actually be void* pointers. But then, the type is lost; to properly get back stored values, it seems one would have to add tagged-union-like tagging --but for pointers.

How would you do that? Various proposals welcome :-)

Side-issue: to cast back elements, I wanted to store the actual element type. But I could not find a way to do that, instead get weird error messages like eg 'int' is not a value (so, what else?). For a typed collection (template), it is well possible to store the element type, like eg:
	struct List(T) {
	    alias T Element;
	    ...
	}
But this indeed gives a typed collection. So, what's the issue indicated by the error?
If I don't have the type, the tag can only be a pseudo-thingie (possibly member of an enum) used with a possibly very long switching sequence. Meaning instead of something like:
	value = *(cast(type*)element);		// type is called 'type'
I need to write:
	if (type == Types.integer)		// enum 'Types'
	    value = *(cast(int*)element);
	else if .......
Very annoying.

Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d-learn mailing list