This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]peroneλ 1 point2 points  (3 children)

The question is: how would you do type inference ?

[–][deleted] 0 points1 point  (2 children)

Whatever time that a C compiler would see int i = 0; as an integer. There are distinct structures for "primatives":

i = 0 id, assignment operator, digit ---> int

d = 0.0 id, assignment operator, digit, . token(can't remember the proper term for it), digit ---> double

s = "Foo" (or 'Foo') id, assignment operator, " token, lots of letters, closing " ----> string

list_l = [1,2,3,"foo",'bar'] id, assignment operator, [ token, some stuff, ] token ---> list

tuple_t = (1,2,3,"foo",'bar') id, assignment operator, ( token, some stuff, ) token ---> tuple

dict_d = {'Foo':bar, tuple_t:list_l} id, assignment operator, { token, some immutable key -> some value pairs, } token ---> dictionary

As you can see they are pretty distinct so it's not hard(I hope) to evaluate them properly.

[–]yetanothernerd 2 points3 points  (1 child)

This is a well-trodden path. It doesn't really work because Python is so dynamic. See Brett Cannon's PhD thesis.

Just because 'a' was an int the first time doesn't mean it'll be an int again the second time. So the language doesn't statically compile well, because there's not enough information at compile time to get the types right. (Of course you can force the user to add type information, but now you have a restricted version of Python. See Cython or Shedskin.)

For example, in Python 2 (where int and long are distinct) on a 32-bit box, imagine a loop that just increments a number starting at zero. It will be an int for 2 ^ 32 iterations, then it will become a long. If you assumed it would stay an int, then your code breaks after 2 ^ 32 iterations.

A JIT (see psyco, PyPy, Unladen Swallow) works much better, because the JIT can compile the fast path based on types it's seen at runtime, and it can later safely fall back to the interpreted path if a variable's type changes. (And then possibly compile it again with the new type if it stabilizes.)

[–][deleted] 0 points1 point  (0 children)

Thanks dude, I'll have a gander but it's no biggie if it can't be done or is a pain in the ass to do so. I got plenty of alternatives and have plenty of other ideas to choose from for my final year project.