Stefan Behnel wrote:
> Dag Sverre Seljebotn wrote:
>> Robert Bradshaw wrote:
>>> On May 7, 2009, at 12:24 AM, Dag Sverre Seljebotn wrote:
>>>> What I'm getting here at is that we are creating a distinction in
>>>> Cython
>>>> which actually does not exist in C++ (rather C++ adds member methods,
>>>> constructors, private members, and vtables all as orthogonal and
>>>> independent extensions).
>>>>
>>>> But I think that is how it should be.
>>>
>>> Yep, if C++ considers them different enough to allocate two separate
>>> keywords for them, than so should we. (I think it'll also be less
>>> confusing).
>>
>> But C++ doesn't!!
>>
>> class Foo {
>> int x;
>> public:
>> int y;
>> void z();
>> };
>>
>> is EXACTLY the same as
>>
>> struct Foo {
>> int y;
>> void z()
>> private:
>> int x;
>> };
>>
>> Whatever the distinction is supposed to be in Cython, it cannot be the
>> default private/public, as only the public stuff is available to us
>> anyway!
>>
>> I've been using the distinction to mean "these structs should be
>> reference-counted, not have stack-allocated semantics like we use for C
>> structs". If one doesn't plan to ever go for a reference counted scheme,
>> we should just use plain struct.
>
> Ok, so these are orthogonal goals, IIUC. In that case, I'd vote for plain
> "struct" to avoid the ambiguity with Python's "class".
>
> Then, struct methods would behave different for C and C++ semantics in
> that the C++ code would call the class method, while the C code would use
> the transformed plain function call. C struct methods can only be defined
> in Cython (.pyx) code, whereas C++ struct methods can only be defined in
> external C++ declarations.
That's a very constructive idea.
But couldn't we just use
cdef extern from "myheader.h":
cdef struct MyStruct:
# External declaration
int cppfunc(self, float)
# Struct convenience wrapper
int cythonwrappingfunc(self):
print self.a
(Here cppfunc has "self" just for visual consistency, it could be dropped.)
> Note that we can always switch to "class" or whatever other declaration
> later on if we want to enable ref-counting and consider it similar enough
> to Python's class behaviour. That would be an additional feature.
I'm a bit wary of encouraging people to start writing
cdef CppClass a
which would then be possible (if and only if the constructor has no
arguments -- which is rather rare anyway in my experience); because it
would start calling constructors and destructors, interfering with our
knowledge of control flow.
A feature of "cppclass" could be that it disallowed code like
cdef CppClass a
with "Error: Cannot allocate C++ object on stack".
Dag Sverre
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev