Stefan Behnel wrote:
> Dag Sverre Seljebotn wrote:
>> However, it seems to require templates/generics in Cython. So that would
>> be the place to start working. Unfortunately that would probably be some
>> work (on the order of a GSoC I think?), but I think that would be a much
>> better approach than hard-coding in a brand new vector type in Cython.
>>
>> (This is when I usually state that I think it is better if we can
>> properly
>> wrap STL though, which will hopefully be done something about in summer,
>> since the list of data structures which it would be nice to have doesn't
>> at all stop with vectors.)
>
> Wrapping the STL sounds like a different feature. I don't think we should
> *require* C++ here. Writing type templates into plain C would be a lot
> more
> useful. It would be more work, sure, but it shouldn't be too hard. It
> would
> mainly require smarter type checks (i.e. MyType<double> != MyType<int>)
> and
> code duplication in the generated C code. Maybe it's not even that hard to
> hack that into the current code.
>
> Imagine you could write
>
>     cdef class MyType<T>(object):
>          # type T gets defined in the class scope to make the parser happy
>          cdef T* my_attribute
>          def __init__(self, T value):
>              self.my_attribute = malloc(sizeof(T)*1000)
>              self.my_attribute[100] = value
>          def T do_work(self, T input):
>              return input + self.my_attribute[100]
>
>     o = MyType<int>(20)   # first occurrence creates/registers the type
>     o.do_work(18)
>
>     ctypedef MyType<int> MyIntType   # reuses the type and provides a name
>
>     cdef MyIntType ot = MyIntType(25)
>     print ot.do_work(19)
>
>     ctypedef MyType<double> MyDType  # creates and defines a new type
>
> I'm not sure if a common base class would make sense here, something that
> would just implement everything that doesn't depend on T. But maybe that's
> overkill already. And most stuff *will*

Hmm. Yes, you are right, there's lots of stuff which doesn't need to be
supported at first. Somebody still has to do it though :-)

Before the C++ syntax sticks though I'd like highlight some benefits of
the [] syntax:

- It is legal Python to do "vector[cython.int]()". (Which leaves the way
open for providing a meta-class doing run-time type-checking of arguments
and return values, which would be the Python run-time compatability layer
in the shadow module. That's far off and perhaps too little useful, but I
like leaving the way open.).
- Guido once used the [] syntax for something similar (which never became
anything) [1]
- Using < and > makes the parser more complicated, while [] must already
be parsed. (in C++ there's the mess of having to write "vector<vector<int>
>", note the space, because there would be some problem with the >>
operator and the parser otherwise).

[1] http://www.artima.com/weblogs/viewpost.jsp?thread=86641

I even think one should use [] for wrapping STL personally.

Dag Sverre

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

Reply via email to