Mixed types and variants

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Nov 23 16:26:10 EST 2005


Notes:
- This email is about Mark Dufour's Shed Skin (SS)
(http://shed-skin.blogspot.com), but the errors/ingenuousness it
contains are mine. My experience with C++ is limited still.
- The following code comes from a discussion with Mark.


One of the purposes of SS is to produce fast-running programs
(compiling a subset of Python code to C++), to do this it accepts some
compromises in the type flexibility used by the programs, at the moment
is doesn't allow mixed types (like mixed typed dicts). A future version
of SS may support variant types, that are usually quite slow, but they
may allow SS to use mixed types. If a SSPython program processes a lot
of fixed typed data and few variants, the speed of this program can be
roughly the same, but the range of programs that SS can successfully
compile can be increased.

A variant type may allow SS to manage a dict like this too:
d = {1:"2", "1":2}
Or a list created dynamically like this:
l = [[1,2], [], [], [[3],[4]], [5,[6,7]]]


You can find some C++ variant types:
cdiggins::any
boost::any
boost::variant

But it seems that these only support 'value types', so how to put a
class pointer in such a variant and dynamically call a method?
Something like this fails:

#include "cdiggins.hpp"
#include <stdio.h>

class bert {
public:
int zooi() { return 345; }
};

int main() {
cdiggins::any a = 42;
a = new bert();
printf("%d\n", a.zooi() );
}


If this is not possible, such variants don't look very useful in
general for SS.

My possible answers:
- "Value" types only can be better than nothing, because such variants
can be used to make mixed dicts, those mixed lists, etc. Some
functionality reduction is better than an even bigger functionality
reduction.
- Maybe the boost::variant sourcecode can be modified to allow such
functionality too, or part of it. But this may require a LOT of work on
the C++ implementation (Mark's idea: maybe a better solution would be
to fall back to CPython classes somehow to implement variants...).
- Maybe someone here can suggest some other variant type, or some other
solution.

Bye and thank you,
bearophile




More information about the Python-list mailing list