1. Scala's static type system
Welcome back! Scala being statically typed is a key feature of the language so let's dive deeper. This lesson will be conceptually-focused.
2. More answers to "Why use Scala?"
Most of this quote from the first video of the course should make sense now. Take a moment to be grateful for how far you've come!
3. More answers to "Why use Scala?"
Let's deconstruct this bolded line. Scala's static types help avoid bugs in complex applications.
4. Some definitions
First, some definitions. Pause the video and read this definition of a type.
5. Scala value types have equivalent Java types
You learned that Scala value types have equivalent Java primitive types and that Scala code is first compiled to Java bytecodes THEN run on a Java virtual machine.
6. Some definitions
Compile time is when Scala code is compiled to Java bytecode. Run time is when that resulting executable code runs on a Java virtual machine. Take a second to pause and absorb if you need to.
7. Type systems
A LANGUAGE is statically typed if the type of a variable is known at compile time. Some examples of languages with static type systems are C/C++, Fortran, Java, and Scala. A language is dynamically typed if types are checked on the fly, after compile time, if compiled. JavaScript, Python, Ruby, and R are examples.
We will flesh out type systems and Scala's type system in particular in more advanced courses. For now, you need to know
8. Pros and cons of static type systems
the pros and cons of static typing. First, since statically typed languages don't HAVE to check types when someone hits "run program", they execute a little faster.
Second, verifiable properties of your program. For our program that plays Twenty-One, we add integers together to get a hand's point value. Static type systems can prove the absence of certain run-time errors, like Booleans not being added to those integers (which is possible!) for example. The properties of our program are verified. Trivial bugs are caught early.
Third, a static type system lets you make changes to a codebase with a high degree of confidence. When you recompile your system after a refactor, Scala will alert you of the lines you tweaked that caused a type error, then you fix them. Engineers working in big codebases LOVE this safety net.
Finally, documentation. Type annotations tell us what we expect a variable, collection, function to do. Unlike a comment, a type annotation is never out of date since the compiler checks it for correctness.
The main cons of static type systems are the time it takes to check types, code becomes more verbose and more annoying to write, and the language is inflexible, preventing coders from expressing themselves as they wish.
Fortunately, Scala has an ADVANCED static type system that addresses the verbosity and inflexibility cons.
9. Reducing verbosity (with variables)
Verboseness through type inference, which you've seen in action with variables, with colon Int removed here.
10. Reducing verbosity (with collections)
You've also seen type inference with collections, here with Array String omitted. If you're ever thinking, "Writing this type is annoying. It's obviously this type!" you can probably skip it because Scala will infer it for you. Voilà! Less verbose code. Here, we shaved off 13 characters, which is a BIG deal when multiplied over thousands of lines of code. That said, there are times when type inference can't and shouldn't be used, though that's a topic for another course.
11. Promoting flexibility
The "language not flexible" con is addressed through pattern matching and various new ways of writing and composing types. Again, advanced topics to learn later.
12. Compiled, statically-typed languages
One final note: Scala being both compiled AND statically-typed doubles down on the "increased performance" benefit. Because the compiler knows the exact data types that are used, it can allocate memory accordingly to make the code faster and memory efficient.
13. Let's practice!
Great work. Static typing may seem fuzzy now, but it will become second nature with experience. Exercises help too!