4 ms·
> 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 expr
by staticassertion 9y 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 9y 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 9y 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 9y 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.
- StefanKarpinski 9y agoJulia is always dynamically typed and type annotations do not make it static, in contrast to how some "traditional" gradually typed research languages have worked. You can find a more detailed explanation here: https://stackoverflow.com/a/28096079/659248 https://stackoverflow.com/a/28096079/659248 The notion that dynamic languages are "unityped" comes from looking at them through the lens of static typing, leading to a disconnect about what "type" means and a correspondingly meaningless answer. Since the static notion of type applies to expressions and the dynamic notion of type applies to values, when you ask what the type of an expression in a dynamic language is, you get an useless answer since dynamic languages don't—by their very nature—assign types to expressions. Yes, you can sometimes figure out what the type of an expression must be, but the ability to do so is incidental and not guaranteed by the language, nor an inherent part of its semantics. The dynamic notion of type belonging to a value distinct from an expressions is present to a limited extent in object-oriented static languages with subtyping: when the static type of an expression and its actual runtime type can be different. The static type corresponds to the static notion of what a type is while the runtime type corresponds to the dynamic notion of what a type is.
- staticassertion 9y agoI don't get where this whole "expressions vs values" thing is coming from. Why are you making this distinction? I don't get it. The expression '2 + 2' has a type in mypy - so why are we drawing this distinction here of all places? > Yes, you can sometimes figure out what the type of an expression must be, but the ability to do so is incidental and not guaranteed by the language, nor an inherent part of its semantics. It's not guaranteed with most languages - they build up from primitives, and almost all of them have some sort of soundness holes. If I have every primitive type in Python correspond to a type in mypy, I don't get how that isn't a basis of a static type system. It seems like you're talking about the difference between evaluation and a value itself. But mypy types are evaluated... > The notion that dynamic languages are "unityped" comes from looking at them through the lens of static typing, leading to a disconnect about what "type" means and a correspondingly meaningless answer. Pretty sure it's just the simple, type-theory way of defining it, and I don't see why we would define types in a way that isn't consistent with type theory.
- catnaroek 9y ago> And, in fact, it has a name - gradual typing. Is there a guarantee that runtime type errors in Julia can be precisely traced back to an offending piece of untyped code? This is part of the definition of what gradual typing is.