[Python-ideas] generic code and dependent types

Dennis Brakhane brakhane at googlemail.com
Fri Aug 15 14:44:04 CEST 2014


Am 15.08.2014 14:26, schrieb Steven D'Aprano:
>
> Let's pretend that this is a Python mailing list, and that some readers 
> aren't familiar with C++ generic syntax :-) What does 
>
> typedef typeof (int() * float()) my_type;
>
> do? 

I think its "whatever type operator*(int a, float b)" returns.

Or more generally: "whatever type a suitable * operator returns after,
where suitable means after int and float might or might not have been
casted into another type so that a matching operator was found"

So, for example, if there were no operator*(int a, float b), it would
look if there's at operator*(int a, int b), and if not, operator*(float,
float) (or vice versa).

The "advantage" is that you do not need to change your code if the
meaning of multiplying an int with a float changes.


More information about the Python-ideas mailing list