[Cython] Fused types syntax

Dag Sverre Seljebotn d.s.seljebotn at astro.uio.no
Fri Jun 3 10:39:42 CEST 2011


On 06/02/2011 11:51 PM, Robert Bradshaw wrote:
> On Thu, Jun 2, 2011 at 2:42 PM, mark florisson
> <markflorisson88 at gmail.com>  wrote:
>> On 2 June 2011 23:31, Robert Bradshaw<robertwb at math.washington.edu>  wrote:
>>> On Thu, Jun 2, 2011 at 2:18 PM, Dag Sverre Seljebotn
>>> <d.s.seljebotn at astro.uio.no>  wrote:
>>>> On 06/02/2011 06:39 PM, Robert Bradshaw wrote:
>>>>>
>>>>> In looking at merging fused types, it's time to nail down the syntax.
>>>>> The current implementation is
>>>>>
>>>>>      ctypedef cython.fused_type(list, dict, object) fused_t
>>>>>
>>>>> This requires an addition to the grammer to allow the "call" syntax in
>>>>> a type declaration, as well as special casing to make it allowed only
>>>>> in a typedef. What about
>>>>>
>>>>>      cython.fused_type[list, dict, object].
>>>>>
>>>>> One advantage is that indexing is already valid in type declarations,
>>>>> and its the typical syntax for parameterized types. Thoughts? Any
>>>>> other ideas?
>>>>
>>>> I don't really like overloading [] even more, and I think () (or, perhaps,
>>>> 'fused_type([list, dict, object])').
>>>>
>>>> But I don't feel very strongly about it.
>>>>
>>>> If you only want this allowed in typedefs, then, being puristic, I think
>>>> that really a "fused type" is really different from a ctypedef, and that it
>>>> would warrant something like a new keyword.
>>>>
>>>> cdef fusedtype [list, dict, object] fused_t
>>>>
>>>> That's rather horrible, but you get the gist. The thing is, once you use a
>>>> ctypeudef, you really should allow
>>>>
>>>> def f(fused_type(float, double) x, fused_type(float, double) y): ...
>>>>
>>>> but then, how many fused types do you have, one or two?
>>>
>>> Two, and you can't refer to them. Such anonymous types could be handy
>>> for one-off functions, e.g.
>>>
>>>     def f(fused_type[list, dict, object] x): ...
>>>
>>> but maybe that's not needed.
>>>
>>>> So this makes it seem to me that using ctypedef is a rather horrible hack.
>>>
>>> Yeah, this is the crux of the issue. (To be clear, Mark's
>>> implementation is good, it's our syntax that's hacky). It's perhaps
>>> less ugly than
>>> introducing a new keyword. A crazy idea:
>>>
>>> cdef fused floating_t:
>>>     float
>>>     double
>>>     long double
>>>
>>> (in analogy with how we declare types like structs and enums).
>>
>> I like that syntax, the bad thing about ctypedef for me is that
>> everything goes on one line, at which point you have to start breaking
>> them if they get too long (which can happen quite easily with lenghty
>> names like 'long double complex'), which looks awkward.
>>
>> So much for trying to reduce grammar changes, though :)
>
> Well, it introduces a new kind of node, rather than the kind of funny
> interaction/restriction with typedef, which got me thinking about this
> in the first place. Of course then you can't create an anonymous one
> for use in a function (which may be viewed as a feature or a defect).

I'm +1 on this syntax, I really like it in fact. It also leaves a lot 
more room for anything else one might need to add in eventually (the 
"paired" idea of Pauli and so on).

And I think not being able to create an anonymous one is a feature.

Dag Sverre


More information about the cython-devel mailing list