digitalmars.D - TypeInfo.next implementation
- Benjamin Thaut (50/50) Feb 19 2013 While working on my TypeInfo comparison pull request I came across the
While working on my TypeInfo comparison pull request I came across the following issue: TypeInfo.next currently is implemented in such a way that elements in the cain that come right after a TypeInfo_Const, TypeInfo_Shared or TypeInfo_Immutable are jumped. This was quite confusing to me, as I assume that next simply returns the next element in the cain. For some situations walking the chain without jumping any elements is neccessary (e.g. comparing TypeInfo objects, formating functions). First consider the following type const(const(int)[]) The type chain will look as follows: const -> array -> const -> int Calling next once with the current behavior will return: const -> int Calling next with the jumping removed will return: array -> const -> int Now I'm currently thinking which of the following options would be the best: For all options I would implement a "unqualified" property for TypeInfo which will return this in all cases but shared, const and immutable. For these it will return the internal base member and thus unqualify the given TypeInfo. Option 1: Leave the old behavior and make the iterating logic more complex. e.g: void iterateTypeInfoChain(const TypeInfo info) { writefln("%s", to!string(info.type)); switch(info.type) { case TypeInfo.Type._const: case TypeInfo.Type._immutable: case TypeInfo.Type._shared: iterateTypeInfoChain(info.unqualified); break; default: iterateTypeInfoChain(info.next); break; } } Option 2: Change the behavior of TypeInfo.next to always return the next element in the chain and then find and fix all places that rely on the old behavior by replacing ".next" with ".unqualified.next" Option 3: Add a "nextTypeInfo" property that will return the next element in the chain and leave the "next" property as is. Iterating logic would then be: void iterateTypeInfoChain(const TypeInfo info) { writefln("%s", to!string(info.type)); iterateTypeInfoChain(info.nextTypeInfo); } For anyone wondering: The iterating logic presented here uses tail recursion because next currently returns a const(TypeInfo). Kind Regards Benjamin Thaut
Feb 19 2013