[Cython] Fused Types

Robert Bradshaw robertwb at math.washington.edu
Thu Apr 28 23:59:13 CEST 2011

On Thu, Apr 28, 2011 at 2:29 PM, mark florisson
<markflorisson88 at gmail.com> wrote:
> On 28 April 2011 22:31, mark florisson <markflorisson88 at gmail.com> wrote:
>> On 28 April 2011 22:12, Robert Bradshaw <robertwb at math.washington.edu> wrote:
>>> On Thu, Apr 28, 2011 at 12:48 PM, mark florisson
>>> <markflorisson88 at gmail.com> wrote:
>>>> So I fixed all that, but I'm currently wondering about the proposed
>>>> cython.typeof(). I believe it currently returns a string with the type
>>>> name, and not the type itself.
>>> Yes. This is just because there's not really anything better to return
>>> at this point. We should "fix" this at some point in the future.
>>>> So I think it would be inconsistent to
>>>> suddenly start allowing comparison with 'is' and 'isinstance' and
>>>> such.
>>> I'm open to other suggestions, but would like an expression that
>>> resolves at compile time to true/false (and we need to do branch
>>> pruning on it). Note that type() is not good enough, because it has
>>> different semantics, i.e.
>>>    cdef object o = []
>>>    typeof(o), type(o)
>>> so lets not touch that one.
>> Right, so for fused types I don't mind string comparison with
>> cython.typeof(), but retrieval of the actual type for casts and
>> declaration remains open. I'd be fine with something like
>> cython.gettype().
> It seems that this isn't optimized yet, but it looks to me like it
> wouldn't be very hard to do so. At least == and != could be resolved
> at compile time if the other operand is a string literal.

Yes. We could consider supporting "typeof(x) is typeof(double)" or
even "typeof(int*)" etc. as well to not tie ourselves to strings. Or
perhaps some other syntax we haven't thought of. Alternatively, it
would be nice if it involved the literal fused_type as that's what
we're really branching on, e.g.

ctypedef cython.fused_type(A, B) AorB

cdef foo(AorB x):
    if AorB[A]:   # or .A or something

it's hard to come up with something that plays nicely with pointer types.

- Robert

More information about the cython-devel mailing list