Type-Erased Container Iteration
Original Publication Date: 2014-Jan-23
Included in the Prior Art Database: 2014-Jan-23
AbstractDisclosed is a method of processing the elements of a container in a strongly typed language without compile-time knowledge of the type of the container or of the element type within the container.
Page 01 of 17
Type- Erased Container Iteration
Disclosed is a method of processing the elements of a container in a strongly typed language without compile-time knowledge of the type of the container or of the element type within the container.
Keywords: Type-erasure, container, iteration, rtti, runtime type information, dynamic type, dynamic typing, variable type, any type
A strong type system provides readability, safety of data interpretation and can be used for compatibility between different code from different vendors. The type of all code components is determined at time of compilation of source code and is immutable. The consumer of an instance of data must know the type of the data in order to process it. Intermediate code may transport references to data, without communicating the type of the data, by using the void pointer type. Such a void pointer may be coupled with an runtime integral value which corresponds to the strict, static type. The integral value may be used at runtime to determine the viability of interpretation of the data as a particular static type. This is already common practice.
Containers are a category of type which refer to a collection of instances of a particular element type. Containers differ in implementation details such as whether elements are stored contiguously in memory, or as a linked list for example. Containers have associated types which may be used to iterate over and process the elements in the array. Such iterator types are strongly coupled to the element type in the container. Thus, to iterate over and process the elements in a container, the type of the container must be statically (ie, at compile time) known and the type of the elements in the container must be statically known.
A runtime registration system is required to assign an integral value to each unique type which may be contained, and each container. This may use``rtti`` (run-time type information) supplied by the compiler, or it may use external scaffolding.
What is disclosed is a method of ``type erasure`` whereby the elements of a container may be iterated over, without static knowledge of the type of the container or the type of the elements of the container.
This method is applicable to a language which:
1) Is statically typed
2) Has a way to represent data whose type is not known (eg a void pointer)
3) Has a way to cast/interpret such non-typed data as a particular static type, where the type is defined at compile-time.
4) Has a common way to represent operations on containers (eg iterators, algorithms) with "static polymorphism" - that is, the API is the same regardless of the precise type of the container or the type of the element in the container.
5) Has no convenient built-in to provide container abstraction features.
That includes C++, but may include other languages.
Page 02 of 17
In the diagram, the strongly typed container is on the left side 'Container Type', and it has an associate...