|
generic_iterators
1.0.0
Demonstration of implementing and using type safe generic iterators in pure, standard C
|
This document aims to describe the typeclass (or trait, or interface) based polymorphism pattern used in this demonstration.
Note: The information in this document is outdated. An extended version of this document can be found at typeclass-interface-pattern.
Alongside describing the core parts of this pattern, this document will also describe how to combine multiple typeclass constraints into one constraint. As in, how you can have a type that is required to implement multiple typeclasses.
Function pointer based polymorphism isn't new to C by any means. The major difference in the typeclass pattern, and the typical vtable based approach is simply that typeclasses are based around actions, rather than objects. This is very similar to an interface in OOP terms.
There are 3 core parts to this pattern. These parts will be demonstrated by implementing the Show typeclass.
typeclass struct definitionThis is the struct containing the function pointers related to the typeclass. For Show, we'll just be using the show function here, it takes in a value of the type for which Show is implemented (i.e self) and returns a printable string.
This can be simplified using the typeclass macro provided in typeclass.h.
A simple struct containing the virtual function(s). When the wrapper function is first called (to convert a certain type to its typeclass instance), a typeclass struct of static storage duration is created with the function pointers for that specific type (a vtable of sorts). The pointer to this struct is then used in all typeclass instances. More on this will be discussed in the impl_ macro part.
typeclass_instance struct definitionThis is the concrete instance to be used as a type constraint. It should contain a pointer to the typeclass, and the self member containing the value to pass to the functions in the typeclass struct.
This can also be simplified using the typeclass_instance macro provided in typeclass.h.
impl_ macro used to implement the typeclassThis macro is the real heavy lifter when it comes to type safety.
It takes in some information about the type you're implementing a typeclass for, and the exact function implementations that will be used for that type, and defines a function which does the following-
Type checks the function implementations given.
This is done by storing the given function implementations into function pointers of an exact and expected type.
self member.Following these rules, this is what impl_show would look like-
It takes the show implementation as its third argument. In the function definition, it stores that impl in a variable of type char* (*const show_)(T e), which is the exact type it should be - T is the specific type the implementation is for. It must be a pointer type. Since it's stored into void* self.
The (void)show_; line is to suppress the unused variable warning emitted by compilers, since show_ isn't actually used. It's only there for typechecking purposes. These 2 typechecking lines will be completely eliminated by any decent compiler.
Then it simply defines a static typeclass and stores the function pointer inside. Then it creates and returns the Showable struct, containing the x argument, and a pointer to the typeclass struct.
Once the typeclass and typeclass instance structs have been defined, all the user has to do is call the impl_ macro with their own type and the function implementation(s) required for the typeclass. The declaration of the function defined by said macro can then be included in a header. After that, that function can be used to turn the concrete type into its typeclass instance.
Here's an example of implementing the previously defined Show typeclass for an enum-
The impl_show macro here, simply translates to-
Now, you can convert an Antioch into a Showable like so-
And this Showable will automatically dispatch to the antioch_show function whenever someone calls the show function inside it.
Now you can make polymorphic functions that works on Showables. Here's one of them-
You can now easily print an Antioch with these abstractions-
Where this really shines though, is when you have multiple types that implement Show - all of them can be used with print. Or any other function that works on a generic Showable!
One of the core design goals of a typeclass is to be modular. A Show typeclass should only have actions directly related to "showing", a Num typeclass should only have actions directly related to numerical operations. Unlike objects, that may contain many different methods of arbitrary relevance to each other.
This means that, more often than not, you'll want a type that can do multiple different classes of actions. A type that implements multiple typeclasses.
You can model that pretty easily with this pattern-
Where Enum is also a typeclass defined like-
Essentially, you can have a struct that stores each of the typeclass pointers you want to combine, and the self member. The impl macro would also be very simple. It should simply define a function that puts the given value into ShowableEnumerable, into self, as well as use the impl functions to get the typeclass instances of that type.
With this, if you implemented Show for Antioch* and defined the function as prep_antioch_show, and also implemented Enum with the function name prep_antioch_enum, you could call impl_show_enum using-
The defined function would have the signature-
That's it!
You can now have functions that require their argument to implement multiple typeclasses-
IteratorFollowing the typeclass pattern - you can make an Iterator where each element is a typeclass instance. The typeclass instance can either just be for one typeclass (like Show), or it can be for multiple typeclasses (like ShowableEnumerable).
Here's a full demonstration all in one snippet, turning an array of Antiochs into an Iterable of Showables-
Note: Show and Antioch here have their origins in Core Parts. You'll remember, the Show impl function for Antioch has the signature- Showable prep_antioch_show(Antioch* x);. The foreach macro is defined in iterutils. It's using the CONCAT macro, which is defined in func_iter.h