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 :-)

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?
>
> 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 :-)

Dag Sverre

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

Reply via email to