What are types ? How does a program go from ones and zeros to the complex data-types that we are used to. And what the hell is a strongly typed language ? Is there really such a thing ? I will try to answer all these questions and hopefully shed a little light on this whole types thing.
Dealing with types
The computer only knows 1 and 0. That is not much info to go around. A type is, fundamentally, a way of looking at a bunch of bits. Let’s take an integer for example. How do you convert that into bits. Well, you can write the value in binary. But that is not the hard part. The hard part is deciding how many bits you should use, make a decision on endianness (but we’re not touching that one for now).
Statically typed vs Dynamic typed
A type is an additional piece of information that needs to be stored for a variable (in addition to the value). And there are two places where that information comes from:
- From the programmer: The source code contains all the information required about types, and you can easily track down and determine the type for each of the variables used in the source code. In addition, the type should not change. I would call these languages statically typed languages. I don’t really agree to the term strongly typed languages, and I think the term statically typed makes more sense here.
- From the runtime: All the type information is determined while the program is running, from context. The runtime keeps track of all the type information and updates that info while the program executes. The type of variable can change while the program executes if it gets assigned a different value type.The programmer does not know how a variable looks like (unless he takes care while assigning values). This is the basic idea behind dynamically typed languages.
Pros and cons
- Statically typed languages are less error prone.
- If you know the type of the variable before the program starts, then you can determine the type of each expression in the program and most importantly, make sure that the expression makes sense.
- Statically typed languages are faster.
- If you know for a fact the types are correct as the code executes, then you don’t need to check. Over time this can add up to quite a few computer cycles.
- Dynamic typed languages are faster to write in.
- Before you start shouting at me, let me add a few caveats to this. I strictly believe that the bulk of effort involved in programming is finding the solution and not necessarily implementing it. However, there are some overheads that come with statically typed languages. Mainly the fact that you need to define various types for your data. These files can add up really fast. In the long run, I think you break even by not having to debug strange data errors. But, for short-lived projects, I think it’s easier to code in dynamically typed languages (I’m looking at you Python)
Here are some common examples of programming languages and their classification:
- Statically typed languages: C/C++, Java, Scala, Rust, Go
Please note that with enough effort you can circumvent the typing system and turn one type into another. For example, you can convert to Object in Java or use void pointers in C++