On Thu, Jun 2, 2011 at 2:42 PM, mark florisson <markflorisson88@gmail.com> wrote:
On 2 June 2011 23:31, Robert Bradshaw <robertwb@math.washington.edu> wrote:
On Thu, Jun 2, 2011 at 2:18 PM, Dag Sverre Seljebotn <d.s.seljebotn@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). - Robert