5 ms·
Dynamic and static languages are fundamentally different. There is no more or less. I've tried to elaborate here why annotating variables with type doesn't mak
by jernfrost 8y ago
Dynamic and static languages are fundamentally different. There is no more or less.
I've tried to elaborate here why annotating variables with type doesn't make a dynamic language static:
https://medium.com/@Jernfrost/dynamically-typed-languages-are-not-what-you-think-ac8d1392b803 https://medium.com/@Jernfrost/dynamically-typed-languages-ar...
You can't look at the presence of type information to determine if a language is static or dynamic. What matters is when and how that type information is used. In static languages expression have type. In dynamic languages values have type.
The implication is that you can't know the type of something in a dynamic language until the expression has been evaluated at run time. With a static language we can determine the type of every expression at compile time, which requires full knowledge of the whole program. It is why dependencies becomes much more problematic in static languages and why they are so poor as glue languages.
- RussianCow 8y ago> With a static language we can determine the type of every expression at compile time, which requires full knowledge of the whole program. It is why dependencies becomes much more problematic in static languages and why they are so poor as glue languages. But you have to have this information anyway, or at least most of it. Even in a dynamically typed language, you have to know what sort of arguments a function expects; otherwise, how can you write any code at all to operate on those values if you don't even know what they are? Static typing just forces you to be more explicit about encoding those constraints. Whether that's worth the tradeoff depends on a lot of factors, but its use as a "glue" language is certainly not one of them. Edit: Moreover, the line between statically typed and dynamically typed languages isn't as well-defined as you claim. For instance, TypeScript is a statically typed language, but it successfully compiles regular JavaScript code because, by default, every expression is given the type `any`. This means you can start out with dynamic typing and add static types progressively. And at that point, how is that any different than Python with mypy[0]? [0]: http://mypy-lang.org/ http://mypy-lang.org/
- goatlover 8y ago> Even in a dynamically typed language, you have to know what sort of arguments a function expects; otherwise, how can you write any code at all to operate on those values if you don't even know what they are? True, but the Smalltalk idea was that you send a message to an object, and that object decides how (or whether) to handle it. So in Ruby, if you send the + method to some kind of number object along with the objects you want added, and the receiving object decides how to perform that addition, assuming the objects you sent can be summed.
- RussianCow 8y agoSure, but even then, you still have to make assumptions about what messages an object accepts. In other words, you have to have some information about the interface of every object.
- UncleEntity 8y ago> Even in a dynamically typed language, you have to know what sort of arguments a function expects; otherwise, how can you write any code at all to operate on those values if you don't even know what they are? Not necessarily... If the value passed to function implements whatever functionality called then its type doesn't really matter. Really the whole theory behind duck typing. >>> i = 1 + "1" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' while javascript (apparently) will assign "11" to i.
- RussianCow 8y agoIf you operate on a value, you are assuming that the value supports that operation, which means you have some information on the interface that the value supports (i.e. a type). If you had absolutely no information about the interface of a value, you could not possibly do anything with it. There is actually an entire category of static type systems called "structural type systems"[0], which is basically duck typing checked at compile-time rather than runtime. [0]: https://en.wikipedia.org/wiki/Structural_type_system https://en.wikipedia.org/wiki/Structural_type_system
- weberc2 8y agoYou seem to contradict yourself. > With a static language we can determine the type of every expression at compile time, which requires full knowledge of the whole program. > Dynamic and static languages are fundamentally different. There is no more or less. For many Python programs, every expression can be determined statically. While this may not be true for all expressions in all programs, surely this means the OP's claim that "Python is becoming more statically typed" is true and your claim that "There is no more or less" is false, no? Besides, there are languages like Go which have dynamic types (interface{}) and which are considered statically typed languages. You might say that the static type is interface{}, but then you could say that every type in an un-annotated Python program is also interface{} (or whatever the equivalent Python type would be). It seems like your arguments hinge on semantic games.
- coldtea 8y ago>Besides, there are languages like Go which have dynamic types (interface{}) and which are considered statically typed languages. And C# has a var type (dynamic) and Dart has gradual typing.
- jakear 8y agovar is type inference, not dynamic typing. Unless something has changed since I last C#’d. The docs seem to say this still holds: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
- coldtea 8y agoI meant this variable declaration: C# Dynamic Type: C# 4.0 (.NET 4.5) introduced a new type that avoids compile time type checking. You have learned about the implicitly typed variable- var in the previous section where the compiler assigns a specific type based on the value of the expression. ... A dynamic type can be defined using the dynamic keyword.Jan 1, 2014
- 8y ago
- staticassertion 8y ago> In static languages expression have type. In dynamic languages values have type. I don't think this is true. In a dynamic language all values, including expressions, share a single type - a union of all possible types. In a static language you can limit which possible types a value may hold. A dynamic language is like if you cast every single type to Any in a static language. In Python this is entirely possible with mypy - a static type system for the Python language, which works through annotations. > in static languages they are used to prevent the compilation of programs containing expressions where types don’t match up. This is already the case with mypy in Python. It seems very much static. It's like everything is statically determined to be Any, then you specify in certain areas a more limited type. In your case with Julia, the type annotations are static. Even if by default Julia is dynamic, it allows for static annotations. I disagree that there is no in-between. I think Julia is actually a great example of an in-between. And, in fact, it has a name - gradual typing.
- spiralx 8y ago> In a dynamic language all values, including expressions, share a single type - a union of all possible types. In a static language you can limit which possible types a value may hold. A dynamic language is like if you cast every single type to Any in a static language. No, you're thinking of a weakly- versus strongly-typed language. Python is dynamically and strongly typed - values have a definite type, but a variable can be assigned a value of any type.
- RussianCow 8y agoI think you misread the OP: they are saying that Python is like a statically typed language in which every value has the type Any. That has nothing to do with strong/weak typing.
- staticassertion 8y agoAs RussianCow said, I think you're misreading my post. A Python variable, from a type perspective, is a variable with a type Any/ a type that is a union of all possible types. Using annotations you can then specify the type. Nothing to do with weak/ strong, which merely imply some level of implicit casting.
- joshuamorton 8y agoSo what this comes down to is that python is not statically typed, but mypy+ python is. And given how mypy works, you can enforce types over only parts of a program. That, to me, sounds like the python ecosystem is becoming more statically typed.