[Cython] Fused Types
Robert Bradshaw
robertwb at math.washington.edu
Tue May 3 18:23:51 CEST 2011
On Tue, May 3, 2011 at 7:06 AM, Dag Sverre Seljebotn
<d.s.seljebotn at astro.uio.no> wrote:
> On 05/03/2011 03:51 PM, Stefan Behnel wrote:
>>
>> mark florisson, 03.05.2011 15:17:
>>>
>>> if you have
>>>
>>> cdef func(floating x, floating y):
>>> ...
>>>
>>> you get a "float, float" version, and a "double, double" version, but
>>> not "float, double" or "double, float".
>>
>> So, what would you have to do in order to get a "float, double" and
>> "double, float" version then? Could you get that with
>>
>> ctypedef fused_type(double, float) floating_df
>> ctypedef fused_type(float, double) floating_fd
>>
>> cdef func(floating_df x, floating_fd y):
>>
>> ?
>
> Well, if you do something like
>
> ctypedef fused_type(float, double) speed_t
> ctypedef fused_type(float, double) acceleration_t
>
> cdef func(speed_t x, acceleration_t y)
>
> then you get 4 specializations. Each new typedef gives a new polymorphic
> type.
Yep.
> OTOH, with
>
> ctypedef speed_t acceleration_t
>
> I guess only 2 specializations.
>
> Treating the typedefs in this way is slightly fishy of course. It may hint
> that "ctypedef" is the wrong way to declare a fused type *shrug*.
True, but if we start supporting nested things, I think this still
makes the most sense. E.g.
ctypedef floating speed_t
ctypedef floating acceleration_t
struct delta:
speed_t v
acceleration_t a
would still be exactly two versions (or three, if we throw long-double
in there), rather than having un-intended combinatorial explosion. The
as-yet-unspecialized version of delta would be delta[floating] or,
possibly, just delta. One could explicitly ask for delta[double] or
delta[float].
In terms of floating_p, note that "floating is double" and "floating_p
is double*" could both make perfect sense for that particular
specialization.
In terms of compiler support, as long as
ctypedef double my_double
produced something "in floating" then I think it could all be done in
a header file.
- Robert
More information about the cython-devel
mailing list