On Wed, Sep 16, 2009 at 11:56 AM, Stefan Behnel <[email protected]> wrote:
>
> Lisandro Dalcin wrote:
>> cdef void foo(a): # "a" is untyped
>> cdef list b
>> b = a # implicit downcast object -> list
>
> I didn't try, but given that you consider this a problem, I assume that
> Cython currently allows this and you get a runtime error here if a is not a
> list, right?
Yes, see yourself:
$ cat tryme.pyx
cdef int foo(a) except -1:
cdef tuple b
b = a
return 0
def test():
foo([])
$ python -c 'import pyximport;pyximport.install(); import tryme; tryme.test()'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "tryme.pyx", line 7, in tryme.test
(/u/dalcinl/.pyxbld/temp.linux-i686-2.6/pyrex/tryme.c:464)
foo([])
File "tryme.pyx", line 3, in tryme.foo
(/u/dalcinl/.pyxbld/temp.linux-i686-2.6/pyrex/tryme.c:414)
b = a
TypeError: Expected tuple, got list
>
> I would expect users to wrap that code by a type test anyway.
>
Yes, I would also expect users (and myself!!) to do that... But users
(and me!) are not real programers: we DO make mistakes, introduce
bugs, and write inefficient code. Could Cython help in such cases?
Yes, it could.. Should it? Yes, it should, at least if asked
explicitly (the last sentences of course just my humble opinion)
> If we start disallowing similar things for str&friends, would it make sense
> to require an explicit cast in the case above? I don't think a warning
> quite fits here.
>
Well, I always asked for warnings, and even these warns not enabled by
default. If you want to be stricter, then I'm +1. You know, "Explicit
is better than implicit" and "Errors should never pass silently" and
"Unless explicitly silenced" and all that.
Stefan, perhaps the bytes/unicode issues are making you realize that
(despite Cython has to but do not want to be typed) there are cases
where strict typing REALLY do make sense? At some point, Cython let
you interact with C, C is (you like it or not) a typed world, and
strict typechecking in C saves you so many times from shooting
yourself in the foot (have you ever tried Fortran 77?)
--
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev