Mixed types and variants

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bearophileHUGS@lycos.com

    #1

    Mixed types and variants

    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.h pp"
    #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

  • Diez B. Roggisch

    #2
    Re: Mixed types and variants

    > If this is not possible, such variants don't look very useful in[color=blue]
    > 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.[/color]

    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

    Comment

    • Tony Nelson

      #3
      Re: Mixed types and variants

      In article <1132781170.172 321.321750@z14g 2000cwz.googleg roups.com>,
      bearophileHUGS@ lycos.com wrote:

      ...[color=blue]
      > - Maybe someone here can suggest some other variant type, or some other
      > solution.[/color]

      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*nlsn ews@georgea*las tname*.com
      ' <http://www.georgeanels on.com/>

      Comment

      Working...