[Cython] Template functions

2013-03-16 Thread John Tyree
There is currently a void in Cython's C++ support with respect to function (not
class) templates. It would be great to have such a thing, dangerous or not, so
I'm proposing something to get things rolling.

Given that function templates are 100% transparent to the caller, it seems that
the only barrier is Cython's type system. Even in the easiest case, where the
function returns a known primitive type for all input, we still can't use it.

template
std::string to_string(T a)

---

from libcpp.string import string as cpp_string

cdef extern from "foo.h" namespace "std":

cpp_string to_string(??? a, ??? b)


We can used fused types if we know that the function is restricted to numeric
types, for example, but in general this is not the case. The only workaround I
currently have is to declare the function N times for N types. This isn't
disastrous, but prevents sharing of code.

As an alternative, what about a dynamic ANY type that uses the fused type
machinery, but always succeeds when specializing? Or perhaps it just shouldn't
be type checked at all? There is always a backend that will generate the type
error and this possibly gives us macro "functions" for free in C.


cdef extern from "foo.h" namespace "std":

cpp_string to_string(cython.any_t a, cython.any_t b)


Pros:
Huge number of functions become accessible from Cython
User explicitly states when a type should be unchecked
Allows mixtures of typed and untyped parameters in a single call

Cons:
Makes determining return types hard in some cases.
Error messages might be difficult to interpret
?
I'm-sure-this-list-should-be-longer


I'll admit I haven't dug very deep as far as the implications of such a thing.
Is it a reasonable idea? What are the major issues with such an approach?

-John
___
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] Template functions

2013-03-17 Thread John Tyree
> 
> I was thinking of something along the lines of
> 
> cdef extern from ...:
> cpp_string to_string[T](T value)
> T my_func[T, S](T a, S b)
> ...
> 
> It's more a question of how to implement it.
> 
> - Robert

Well this closely matches the syntax used for classes and won't require any type
inference, since the user supplies the type at the call site (am I reading that
correctly?) so I'm not sure what about it will be particularly challenging.

If it's done this way the compiler could generate prototypes as necessary in a
preprocessing step, without inferring anything about the types until later when
overloading is resolved. That feels kind of hacky to me, but I've never written
a compiler with the size and scope of Cython, maybe it's not too bad.  This is
essentially what the user has to do already, and it "works".

The biggest complaint I have about this method is that without inference it
looks like it could lead to a *lot* of extra writing out of types. I'm dreading
the thought of writing out nested template types when calling factory functions
like those in the thrust library, which was what motivated this in the first
place.

-John
___
cython-devel mailing list
cython-devel@python.org
http://mail.python.org/mailman/listinfo/cython-devel