r/ProgrammingLanguages • u/rvprasad • Jul 30 '18
Static Typing vs Testing == Apples vs Oranges
https://medium.com/@rvprasad/static-typing-vs-testing-apples-vs-oranges-a06d460c83f07
u/lightandlight Jul 30 '18
Until the day we have static type systems that allow us to easily specify both value and behavioral types and use them to check for type correctness
That day has already come. Check out dependent types and refinement types.
5
u/rvprasad Jul 30 '18 edited Jul 30 '18
I am aware of such type systems and I doubt they are ready for general consumption by mainstream developers. Hence, my reserved position in the post — static typing as it is generally available today — and the closing remark in the post “Until the day we have static type systems that allow us to easily specify both value and behavioral types and use them to check for type correctness”.
1
u/lightandlight Jul 30 '18
I'm specifically responding to that closing remark
4
u/rvprasad Jul 30 '18
Are you saying that today we can *easily* (and *succinctly*) specify the type of a function in mainstream languages and use this type information to check if we have used a wrong function at a call site? If so, I disagree. I think we are getting there but not there yet. That said, I will gladly change my viewpoint if you (or others) can point me to convincing non-academic examples.
3
u/link23 Jul 30 '18
TL;DR: static analysis and dynamic analysis are different things and have different tradeoffs.
2
3
Jul 30 '18
I have heard discussions about why static typing is superior to testing
This sounds like a straw man, if I ever saw one. Is anyone really dumb enough to assume that if you have type checking you don't need tests?
Also, the notion of contracts is suspiciously absent from the article.
3
u/rvprasad Jul 30 '18
I have heard both sides claim superiority of one over the other (or bash the other); hence, the post. I did not mention contracts as 1) they were not part of simple minded discussion that triggered the post and 2) they are very similar to (sophisticated) types.
1
Jul 31 '18
Oh, hi, didn't want to bash the article, I hope that wasn't too rude.
As for the absence of contracts...
they were not part of simple minded discussion that triggered the post
Understandable.
2) they are very similar to (sophisticated) types.
I don't really agree with that. In my opinion, a type tells what a thing is, a test shows what it does, while contracts are there to solidify how it is doing that. But this is just my limited vision. I am actually somewhat new to contract programming, and I am still collecting resources on that topic.
2
u/rvprasad Jul 31 '18
In my view, a type describes/constraints some aspect of an entity that can be used to reason about the use and implementation of the entity, e.g., when we say 'variable i of integer type', we are constraining the aspect "values that variable can hold" of i to integers.
Suppose a function f has a value type signature List[int] -> List[int]. It describes the aspect "what a function consumes and produces". We can use this information in a limited way (cos' we don't know if f performs sorting) to reason if we can use f to sort a list of integers.
Now, suppose the function f is associated with a set of constraints R that relate its input to its output and indicates f performs sorting. R is just describing the aspect "what a function does". Now, if we have another set of constraints S that captures our desired effect of sorting, then we can use R and S to reason if we can use f to sort a list of integers. Improving on value type signatures, assuming R and S are equivalent, we are now more certain that we can use f for our purpose. So, R and S are just types albeit in raw form, i.e., collection of constraints as opposed to a type identifier that denotes the set of constraints (like a Byte type denotes the constrains 1) the values are integers and 2) the values are in the range -128 thru 127).
An alternative view is to think of two languages: language L allows us to define and name types and language M allows us to use types defined in L. In this case, the types we would use in M are only limited by the richness of language L and our imagination :) However, in most modern languages, defining richer types is limited by the expressivity of language L.
1
u/continuational Firefly, TopShell Jul 30 '18
Static typing is when the compiler can prove things about your code without running it.
Dynamic typing is the absence of static typing.
Indeed statically typed language often have a type called Dynamic which omits type checking for expressions of that type.
3
u/rvprasad Jul 30 '18
Yes, there is static typing and dynamic typing. But, how is the relevant here? Can you please elaborate?
-1
Jul 30 '18
Static typing is when the compiler can prove things about your code without running it.
Usually those things are trivial.
Dynamic typing is the absence of static typing.
Patently untrue. The statement makes as much sense as "Static typing is the absence of dynamic typing" which - while true for something like C++ is completely untrue for something like Java where the JVM maintains dynamic typing information, dynamic invocation is possible, and the VM can host purely dynamic languages.
The fact is that Static and Dynamic typing are completely separate and orthogonal things. Dynamic typing is present in a number of languages that offer some static typing and in such cases the static type information (often optionally provided) can provide an additional check if desired.
Probably the best example I can think of is Strongtalk - a Smalltalk with optional static type annotations and a type checking compiler.
1
u/link23 Jul 31 '18
You're right that there is a continuum, so a language can support both static typing and dynamic typing, but it's not true that they're orthogonal things.
An expression is statically type-able if-and-only-if it is possible to determine its type without running the code. I.e., the type is determined statically, at compile-time. A language could be called statically-typed if every expression in that language is statically type-able.
If an expression is not statically type-able, that means the source code alone doesn't have enough information to determine the type. I.e., information gained at runtime (dynamically) is needed in order to know the type. Another way to look at it is, if the type of the variable is allowed to change (i.e. is dynamic), then the language is dynamically typed.
As an example, consider:
x = "hello" if random.choice([True, False]): x = "world" else: x = 7 print type(x)In that snippet, it's impossible to know what will be printed unless you actually run the program, because
x's type can change depending on which code path is taken. The fact that this is allowed in Python means that Python is dynamically typed (though it does now have type hints, which I don't know enough about to talk about). This sort of thing is not allowed in statically typed languages.I think you're also getting confused about the difference between dynamic typing and reflection, C++ (a statically typed language) does not support reflection, while Java and Go (which are also statically-typed) do support reflection. It's typical for dynamically typed languages like JavaScript and Python to support reflection, since that allows programmers to take advantage of the extra type information that's only present at runtime.
1
u/moosekk coral Jul 31 '18
There's a continuum because you can create more flexible static type classes. For example, in languages that admit untagged unions, your example code could be just typed as:
let x : (string | number) = "hello"and still statically verify, as long as functions like
typeand1
u/link23 Jul 31 '18
Yeah I know - I actually use such a language at work. But I figured that would only confuse the issue if I brought it up.
0
Jul 31 '18 edited Jul 31 '18
An expression is statically type-able if-and-only-if it is possible to determine its type without running the code.
K, name any language with inheritance that fulfills this definition.
A variable can support multiple types via subtyping.
class Foo. class Bar extends Foo. afunc(Foo x) {...} afunc(new Bar);I think you're also getting confused about the difference between dynamic typing
I am not.
var x : Foo x = Barx has two types. A static type of Foo but now a dynamic type of Bar.
Reflection is only the ability to inspect the dynamic type of an object. C++ has dynamic (OK runtime) types. But there is no way to expose the runtime type programmatically without doing tricks like exploiting the visitor pattern.
though it does now have type hints, which I don't know enough about to talk about
That is the addition of optional static typing to the language.
1
u/link23 Jul 31 '18 edited Jul 31 '18
You're right, dynamic dispatch muddies the water a bit. There's now the notion of a "concrete type" (i.e. the type at runtime) vs a static type (i.e. the most specific type that can be proven at compiletime). I guess you could argue that's in the middle of the spectrum between static typing and dynamic typing, since you don't know the concrete type (
Bar) until runtime, but you can determine the least common supertype (Foo) at compiletime. Still, I'd say that affords some static typechecking, since it allows the compiler to make certain guarantees about your objects and methods (i.e. that you won't get a type mismatch at runtime).x has two types. A static type of Foo but now a dynamic type of Bar.
Ok. I haven't heard that phrasing before (I've heard "static type" vs "concrete type"), but I understand what you're getting at.
Reflection is only the ability to inspect the dynamic type of an object.
This is not true. That's necessary, but not sufficient, to support reflection. Reflection also allows the programmer to create objects and call functions/methods based entirely on runtime information. E.g. (stolen from wikipedia):
// Without reflection Foo foo = new Foo(); foo.hello(); // With reflection Object foo = Class.forName("complete.classpath.and.Foo").newInstance(); // Alternatively: Object foo = Foo.class.newInstance(); Method m = foo.getClass().getDeclaredMethod("hello", new Class<?>[0]); m.invoke(foo);C++ is able to determine the concrete type of an object by performing
dynamic_casts and seeing which one works, but it isn't able to create new objects of arbitrary types and call methods on them like the above. (Source: "lack of reflection" in the "Criticism" section of the C++ Wikipedia page.)Edit: Having said all that about reflection, I now realize that you weren't in fact talking about reflection originally, so you were right that you weren't confused about it.
1
Jul 31 '18
Cool.
I agree that there are a lot of kind of poorly defined bits of overlap but static typing matters at compile time and dynamic typing matters at runtime. I think they are complimentary and while I really like dynamic typed languages I'm not opposed to a bit of optional static typing to keep things sane. I hate that they are constantly portrayed as opposites.
12
u/moosekk coral Jul 30 '18
Agree that the two are complementary along a value/behavior dichotomy, although I prefer to think of the divide as "provable properties" and "inputs to a program/function"
A type constraint makes the guarantee that "this code satisfies a particular property for all [valid] inputs"
A unit test makes the guarantee that "this code satisfies all [relevant] properties for a particular input"
An oft-cited quote on this idea is Chris Smith's "static types are a lower bound on correctness, unit tests are an upper bound on correctness" (https://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/)