On Apr 24, 2009, at 1:24 AM, Dag Sverre Seljebotn wrote:

> BTW, sorry to go off in a digression like this -- it is just  
> something a
> problem I remembered with this and I am writing as much to Danilo and
> Robert as you. At least I also answered your question :-)

Yep, this is a good discussion to be having right now...

> Some more thoughts:
>
>> Roland Schulz wrote:
>>> Hi,
>>>
>>> is it possible (or planned) to add operators to a ctypdef struct?
>>
>> Better C++ support is definitely planned; in fact there is a GSoC
>> project on it where this could likely be a part (though Danilo or  
>> Robert
>> would have to comment on that).
>>
>> I started a wiki page listing the things that could be improved  
>> for C++
>> support and
>>
>> http://wiki.cython.org/enhancements/cpp
>>
>> but I became exhausted right before fixing up the operator  
>> overloading
>> section; perhaps you could have a look and come with suggestions?

It would be good, Danilo, if you could flesh this out some more as a  
starting point (including tagging priorities and approximate  
timelines for the stuff you plan on doing as part of your project).

>> Especially, I'm concerned with operators typically returning  
>> references
>> like operator[]:
>>
>> cdef struct Polygon:
>>      Point& operator[](int edge)
>>
>> where one would like to be able to do
>>
>> mypoly[3] = mypoint
>>
>> However this also contradicts the scetched solution for dealing with
>> references which means having Cython treat references as pointers;
>> making it necesarry to do
>>
>> mypoly[3][0] = mypoint
>>
>> Perhaps some map between Python's __setitem__ and reference return
>> values in C++ operator[], but it gets messy syntax-wise quickly...
>
> I should probably provide some more context here. In Python there's
> getitem and setitem as seperate operators. In C++ you would instead  
> return
> an object with overridden assignment operator (for which there is no
> Python equivalent, of course, as assignments are not allowed to have
> side-effects in Python).
>
> So in the example
>
> Point& operator[](int edge);
>
> if you wanted to print something when changing a point, you could do
>
> PointInPolygon operator[](int edge);
>
> instead, and PointInPolygon would contain both a reference to the  
> Polygon
> and the index of the edge, and have overloaded "operator=" and  
> "operator
> Point" which would work as setitem and getitem, respecticely.
>
> So what I'm saying is: Mapping C++ operators to Python operators  
> and vice
> versa is difficult, with possibly many special cases where e.g. a  
> Python
> setitem operator would map to an entire C++ expression involving many
> operators.
>
> Starting out with the binary operators only is simpler and probably  
> a good
> idea :-)

Perhaps I'm taking a too simplistic view of things, but it seems that  
all we have to do is figure out whether or not we can pass a given  
operator onto the C++ code, which is much easier to reason about.  
Also, because in Python assignment is not an expression, we don't  
have to worry about its return value.

The point about handling references (esp. with regard to set- vs.  
getitem still holds, I'm not sure the best way to handle this.

- Robert


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to