[Cython] Fused types syntax

Robert Bradshaw robertwb at math.washington.edu
Thu Jun 2 23:31:28 CEST 2011


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

> But, like I said, I don't feel strongly about this.
>
>
>>
>> P.S. Anyone remember buffers and C++ templated types are dissallowed
>> as typedefs?
>
> As for buffers I just think I never got around to it...

And in that case you can't just punt the typedef to C :).

- Robert


More information about the cython-devel mailing list