Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I believe GP means that they are not checked at runtime. But I don't think that matters much in case of finding bugs, because you run mypy and it will scream that you are assigning wrong type.

I use it whenever I can and it helped me find many bugs in the code (especially not checking if value is None through the Optional). It also makes refactoring your code in IDEs that understand types (like PyCharm) much much easier.



> I believe GP means that they are not checked at runtime.

Haskell types aren't checked at runtime either, so that can't be it.

Python has runtime type checks that do happen at runtime, so maybe it's Haskell that doesn't have types.


What Haskell does and other statically compiled languages is figuring the type before program runs. Once it does it is impossible to have a different type in the code.

Python is dynamically typed. That means everything including things like string or integer is stored as a structure with a type. The checking of the type happens at runtime. This is major reason why languages like Python are so much slower than statically typed languages.

The annotated type in Python is providing mechanism of figuring out types before code is run, this helps finding bugs, but these types are not used during normal operation[1], but that doesn't stop them from being useful for finding bugs in the code or helping with refactoring.

[1] There are some packages that implement some runtime checks, but they IMO are waste of time. Basically they are ensuring that type problems that tool like mypy would detect also will cause your code to crash. It also add performance penalty as well.

There's also mypyc, code that compiles python code that using types increasing its performance. It is currently used to compile mypy increasing its performance by a factor of 4 I believe. It also makes some python features unavailable if you want to use it (https://mypyc.readthedocs.io/en/latest/differences_from_pyth...)


> The checking of the type happens at runtime.

Mypy checks the type before runtime, just like GHC does.

Python also checks during runtime, but Haskell doesn't have that feature. Right?

> This is major reason why languages like Python are so much slower than statically typed languages.

Julia's dynamic typing, and hence its ability to do specialization using runtime information, is part of why it can (sometimes) beat Fortran in numerical performance.


> Python also checks during runtime, but Haskell doesn't have that feature. Right?

I don't know Haskell so can't answer that, but typically a statically typed language doesn't need that.

Sometimes languages still provide runtime check, typically happens when their type system is lacking. For example in Go, if you use interface{} type, the type checking happens at runtime. That's why it is discouraged to use, because could be reason that slows down your code.

> Julia's dynamic typing, and hence its ability to do specialization using runtime information, is part of why it can (sometimes) beat Fortran in numerical performance.

I'm also not familiar with Julia, but from what I read is that for numerical types, Julia uses native types instead of using objects like Python.

It looks like well written Julia code can enable the interpreter to infer types and then JIT can use that to optimize the code. Python doesn't have that functionality at least not yet.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: