The main advantage of static typing to me is that it enables automatic refactorings. Without types, it's impossible for tools to refactor your code safely without the supervision of a human. This leads to developers being afraid of refactoring and, ultimately, code bases rot and become huge piles of spaghetti that nobody wants to touch. With a statically typed language, I'm never afraid to refactor whenever I see an…
I hear this argument a lot, and I'm sure static typing is helpful when refactoring, but I have found that nothing is as important as tests when refactoring. I'd rather refactor dynamically typed code with good test coverage than statically typed code without good tests.
The benefits of static typing without static typing in Python
51–60 of 68 posts
Re: The benefits of static typing without static typing in Python
#52Ugh. I have hardly any type problems in my Ruby code. I've gotten very good at recognizing implicit state and capturing it in a properly instantiated object with a well-named class. I daresay that if you don't have this skill, a type system isn't going to help you much and you're going to get nasty bugs anyway. The problem in Ruby is nils, and you'd have the same problem with the same solution in a static language; c…
Nils are a problem in languages like C and Java, but more powerful type systems completely eradicate the issue. You might enjoy taking a look at Crystal. It has Ruby-like syntax and a type system that makes nil-checks completely unnecessary.
A type system is just OOP with added math. The benefits don't outweigh the hassle. I've tried Crystal. Did not like. It was the language that taught me that there's more to Ruby than nice syntax.
Re: The benefits of static typing without static typing in Python
#53Having type annotations that are ignored at runtime would cause problems when you interact with code without type annotations, no? PHP has a (unfortunately quite limited) set of type annotations, but the interpreter actually enforces them.
How could adding ignored type annotations cause problems? Any problems that arise would have to be there without the type annotations...?
Re: The benefits of static typing without static typing in Python
#54Optional typing like the one we see here is not uncommon in other dynamic languages: For instance, Clojure has core.typed and prismatic schema, which approach the problem in ways related to what the article shows. However, while optional typing gives you some benefits over purely dynamic typing, the fact that it's all bolted-on causes a variety of problems. First, there's the fact that you'll have code with types int…
It's more helpful to think about classes of errors and what the type system prevents. A simple type system prevents type mismatch errors. A more complex type system like Haskell's might be able to encode interfaces and other rules but might also has it's limits. The trade-off is usually that the more classes of errors you remove, the more complex the type-system becomes. Over the lifetime of your program, how much ti…
Re: The benefits of static typing without static typing in Python
#55The syntax is backwards compatible, and this doesn't fit the language well. Forward references to types are handled via a really tacky kludge: "When a type hint contains names that have not been defined yet, that definition may be expressed as a string literal, to be resolved later." So, sometimes you write
def foo(item: 'Tree'):
instead of def foo(item: Tree):
It's not stated in what scope 'Tree' is evaluated. So don't reuse type names.Some type info is in comments:
with frobnicate() as foo: # type: int
# Here foo is an int
Also, Python is getting header files (called "stub" files), like C. In most modern languages, such as Go and Rust, that's all handled automatically by storing type info in the object files. But in future Python, that will be manual.There's function overloading, sort of. There's "@overload", but it doesn't really do anything at run time yet.
This whole thing is a collection of hacks in search of an architecture. This is the messiest type system of any mainstream language. Well designed type systems are hard enough. This is not one of them.
If this hadn't come from Python's little tin god, it would have been laughed out of the Python community. Stop him before he kills again.
Re: The benefits of static typing without static typing in Python
#56Earlier quoted context omitted.
In addition to what the others said, I'd much rather prefer good static typing (i.e. Haskell). Especially in the prototyping phase, I do a lot of iterating and refactoring - and I mean A LOT, because I've found that this is the best way for a good design to fall out. Sometimes like 20-30% of the code get refactored, replacing core data structures and control flow. If I ever had to rely on unit tests for this, I'd sho…
What about tech that generated tests automatically from code and annotations? How do you think that would affect your work?
Re: The benefits of static typing without static typing in Python
#57Earlier quoted context omitted.
I hear this argument a lot, and I'm sure static typing is helpful when refactoring, but I have found that nothing is as important as tests when refactoring. I'd rather refactor dynamically typed code with good test coverage than statically typed code without good tests.
I thought this until I started using a good type system. Java-style type systems don't replace many tests, but have you worked in Haskell / Scala / similar?
Re: The benefits of static typing without static typing in Python
#58Earlier quoted context omitted.
Nils are a problem in languages like C and Java, but more powerful type systems completely eradicate the issue. You might enjoy taking a look at Crystal. It has Ruby-like syntax and a type system that makes nil-checks completely unnecessary.
Nils are only a problem when you're dealing with unclean data. The only way to eradicate the issue is to only deal with clean data. Otherwise you have to code the system to be able to handle it, whether through the type system or in another way. A type system helps, but so does discipline. I'll trade the productivity gains of dynamic typing over a babysitter any day. A type system is just OOP with added math. The ben…
Re: The benefits of static typing without static typing in Python
#59Earlier quoted context omitted.
What about tech that generated tests automatically from code and annotations? How do you think that would affect your work?
Well, this is basically what types are :)
And, again, I'm talking traditional types like in Java or C++ rather than dependent types and other esoteric stuff.
Re: The benefits of static typing without static typing in Python
#60Earlier quoted context omitted.
It is better with certain languages, and it is better when you encode business logic into your type system.
I work on Python and the Pandas data frame is one of the core data structure that is used in the implementation. I have been thinking of how I can translate this code in a more robust type checking language such as Haskell, without loosing some of the conveniences Pandas provides. Do you have any suggestions or pointers to this? I would love to encode the business logic into types. But, I am always afraid I might end…
Edit: In fact, there's this library! https://github.com/acowley/Frames