Fw: [C++-sig] Pyste and STL types

Nicodemus nicodemus at globalite.com.br
Wed Mar 19 05:56:46 CET 2003


David Abrahams wrote:

>Nicodemus <nicodemus at globalite.com.br> writes:
>  
>
>>What am I missing?
>>    
>>
>
>Nothing; I am just suggesting that the Python interface ought to work
>like the first example when the union is a member of another class, as
>it commonly will be.  In order to handle a union coherently there
>must be some additional data telling you which field is active, so
>the union is commonly packaged in a struct:
>
>    struct Test
>    {
>        bool is_float;
>        union val
>        {
>            float d;
>            char* s;
>        };
>    };
>
>In Python, we ought to be able to say:
>
>   Test.val = 3.14
>
>or
>
>   Test.val = 'hello'
>    
>...just IMO.
>  
>

Now I see what you mean. Indeed, that would be a nice interface.
One problem that I see is that there's no way to know when the "active" 
member changes in the C++ side, unless the user gives a mechanism that 
allows us to know, like you suggested. But then the user would have to 
change the wrapping code to update this mechanism. Consider this:

struct Test
{
    bool is_float;
    union
    {
        float d;
        char* s;
    } val;
};

void use(Test& t)
{
    t.val.d = 3.0;
}

There's no way to know that now the active member is "d". The user would 
have to change it to:

void use(Test& t)
{
    t.val.d = 3.0;
    t.is_float = true;
}

(unless this is a common practice: like I said before, I know little 
about unions). So I don't think this solution is optimal. Sure, the user 
could provide wrappers for functions like "use", but it's a lot of work 
compared to the gains, in my opinion. Given this, I think exporting it 
to python as it is seen in C++ is enough. Don't get me wrong: I would 
gladly implement in Pyste a system to export unions like you suggested 
(I'm not lazy), but I don't know if it is possible to do it in a 
reasonable way for the user.






More information about the Cplusplus-sig mailing list