Robert Bradshaw wrote:
> On May 5, 2009, at 10:23 PM, Dag Sverre Seljebotn wrote:
> 
>> Stefan Behnel wrote:
>>> Lisandro Dalcin wrote:
>>>> On Tue, May 5, 2009 at 6:28 PM, Stefan Behnel wrote:
>>>>> I meant
>>>>>
>>>>>    cdef MyType(object) [(T,V)]:
>>>>>
>>>>> here, although I now noticed that there is already the "private  
>>>>> type"
>>>>> syntax:
>>>>>
>>>>> cdef public class _Document [ type LxmlDocumentType, object
>>>>> LxmlDocument ]:
>>>>>    ...
>>>>>
>>>>> so this won't work straight away either...
>>>>>
>>>> But this would work anyway, right?
>>> Not that easily. You could have both [] parts next to each other,  
>>> or just
>>> any one of them. So the parser would fail if you decided to call your
>>> template variable "type", which isn't that a bad name in this  
>>> context.
>> How about
>>
>> cdef class A:
>>     T, V = cython.template_args(type, type)
>>
>> ?
> 
> I prefer the MyType[T,V] syntax. It encapsulates the "meta-type"  
> idea, and also the same syntax can be used both in declaring the type  
> and instantiating the type.
> 
>> (The reason for the arguments: In C++ you can have stuff like ints in
>> template arguments as well, might as well leave the possibility  
>> open even
>> if only passing type is allowed for now).
>>
>> This even puts T and V in the proper scope for @cython.locals
>> declarations, e.g. this passes through (and could even be made to  
>> do the
>> checks) in pure Python:
>>
>> @cython.cdef
>> class A:
>>     T, V = template_args(type, type)
>>     @cython.locals(a=T, b=v)
>>     def foo(self, a, b): ...
> 
> I'd rather see the pure Python mode decorate the class than have a  
> magic method, but I don't think decorators get applied soon enough to  
> inject variables into the scope.

That is correct. The whole advantage was that T and V gets injected in 
the scope in the right place.

A better example is once we start to support argument decorators:

class A:
   T, V = template_args(type, type)
   def foo(self, a: T, b: V):
     ...

This currently passes just fine in Python 3.

We could even allow referring to T and V as self.T and self.V within 
functions (in addition or as the only way -- it is more Pythonic as it 
doesn't invent a template-only scope).

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

Reply via email to