7 - Types
Learning Objectives :
- Learn what a typesysten is
- Learn why a typesystem is helpful to you
- Learn what a types is
- Learn more about objects
- Learn the built-in types in Fantom
- Learn about Fantom's generic types
- Learn about immutability and its benefits
We have covered alot about the Fantom programming language. We are diving into more advance concepts that will make our programs more useful and interesting. At this point you want to make sure you know the material covered in the previous chapters. Again the examples will help you develop your skills in the concepts covered in those chapters. In this chapters we will look at typesystems and types. Explore the advantages and disadvantages of different typesystem. We will examine at Fantom's typesystem and Fantoms basic types. If you are ready let's get started.
Fundamentals of Types and Type Systems
Let's say we have two types of fruits, the real fruits and the fake fruits.
Now let's say that you have a friend that is super dumb and doesn't know anything. The only way he can do stuff is if you give him instructions and directions. Now let's say that you telling your friend what to eat. Let's say you don't aknowledge the differences between the different types of fruit. You don't care if you give him real fruit or fake fruit. You tell your friend to eat any of the fruit from the baskets. Your friend might get lucky and never eat the fake fruit or he may not and die.
Then you show him how to grow him some fruit. He doesn't know you can't grow fake fruit and since you dont care if its fake or real fruit you dont tell him you can't grow fake fruit. Now he will have a problem if fake fruit isn't growing for him.
Now let's say you do aknowledge the different kinds of fruit. You do care if you give him real of fake fruit. obviously you will tell him he can't eat fake fruit. Then you also tell him you can't grow fake fruit either. Since you layed down those rules of don't eat or
grow fake fruit then your friend didn't have to have any problems.
Typesystem work in a similar way. You represented the compiler. Types in a program are represented by the different types of fruit and the computer is the dumb friend. What really are types?**Types are classifications of data**. There can be many different types of data in a program. Typesystems are a set of rules on what can and cannot be done with different data types. Without typesystems programs would have lots of errors and safety issues.
Programming languages can be strongly-typed or weakly-typed languages. Strongly-typed languages have stricter rules on what can or cannot be done on a type. They are the more careful people that care if you give your dumb friend fake or real fruit. They help programmers make less mistakes and make programs secure. They also can make programs less flexible.**Weakly-typed languages have looser rules on what can or cannot be dont on a type**. These can make programs more flexible, but they allow programmers to make mistakes.
There are two way programming languages can check that types are used correctly. One way is static type checking which is when types are checked when the program is compiled. Usually statically checked language have you explicityly specify what type a contruct is (or is using) in the source code, but that is not always the case. Languages can also infer types being used and still be a statically typed language. Static checking makes finding errors easily but makes compile time and development slower. The other way is dynamic type checking which is when types are checked when the program is running. You dont have to explicityly specify what type a contruct is (or is using) in the source code. This makes development and compile time faster. The downside is most program errors aren't caught early and can be hard to debug. Most compiled languages are use static checking and most interpreted languages are dynamically checked.
Now that you know the fundamentals of types and typesystems let's explore Fantom's types and typesystem.