3 ms·
That variance example seems contrived. I'm expected to believe that the programmer explicitly asks for integers in a dynamically-typed language and then is surp
by akkartik 15y ago
That variance example seems contrived. I'm expected to believe that the programmer explicitly asks for integers in a dynamically-typed language and then is surprised by integer overflow?
"in a statically typed language the language would force.. coercion"
But R just did an implicit coercion, right there. You gave it ints, it gave you back 1e12. I don't know R well enough to understand what the int() call does, but there's no reason why a dynamic language can't perform implicit coercion. Python does:
>>> type(1000000)
<type 'int'>
[define sumXX]
>>> sumXX([1000000,2000000,3000000,4000000,5000000])
55000000000000L # <--- type long
I could wrap the elements of the list in int() calls but that would change nothing.
The whole point of dynamic languages is that you don't have to think about the types of your numbers. They're just numbers. If you have a use case that requires "4-byte integers, goddammit!" (and there are many valid ones), why yes, you should use a static language and think about overflow. If you don't want to think about overflow, the languages that go to the greatest lengths to shield you from it (lisps) are all dynamically typed.
- chc 15y agoAlthough many static languages do force you to choose a width for your integers, there is no inherent connection between static type systems and fixed-width integers. For instance, Haskell is one of the most strongly static languages out there and it has an unbounded Integer type. The connection between static typing and integer overflows is an artifact of older statically typed languages like C whose type systems were designed to model the hardware.
- akkartik 15y agoAh, good point. So the whole notion of unboxed integers seems increasingly orthogonal to dynamic typing. And now so do explicit variable declarations (http://news.ycombinator.com/item?id=3633520 http://news.ycombinator.com/item?id=3633520).