Mixed types and variants

B

bearophileHUGS

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
 
D

Diez B. Roggisch

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.

The problem is that C++ has no means of a dynamic method invocation. So
I guess the only thing that helps is to use some variant that has a
PyObject-type, and where calls on values of that type are basically
translated to python C-Api calls, delegating the execution to python
itself. Problem: return types aren't known - either handle them as
PyObjects, or have some cast/hinting in place that reduces them to known
native types like uniform lists.

my .2ç

Diez
 
T

Tony Nelson

...
- Maybe someone here can suggest some other variant type, or some other
solution.

Pyrex? Pyrex is mostly like Python with the possibility of C types. It
handles mixed types just like Python, and the C code it produces is sort
of readable.
________________________________________________________________________
TonyN.:' *firstname*nlsnews@georgea*lastname*.com
' <http://www.georgeanelson.com/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top