Type Systems

This page talks about type systems and errors that can occour.

In staticly typed languages, like C# and C++, you must tell the compiler both the type and name the first time you mention it. We call this declaring a variable. Variables are also bounded by scope, which will be mentioned later down this page. Typing is a helpful feature, as it allows us to deal with large code-bases more easily, can help us spot errors ealier, and gives us some useful features like overloading. It does result in slightly more complexity when using the language.

Declaring Variables

You declare a variable by writing its type before the variable name1. Usually on a line by itself.

To declare a variable named ‘myVar’ of type int, you would write:

int myVar;

You may also provide an initial value for this variable. The following declares a variable named myVar2 and sets its initial value to be 10.

int myVar2 = 10;

You only mention the type when the variable is declared (when you tell the compiler/intpreter to make a new one), not on every use (that would be extra typing, with no real benefit). Once a variable is declared, it will forever be that type.

For example, the following code block is valid:

int myVar = 6;
int myOtherVar = 10;
myVar = myVar + myOtherVar;

On the first two lines we declare two variables, both of type int. One is named myVar and the other is named myOtherVar. We are allowed to provide an initial value to the variable. I set the value of myVar to be 6, and the value of myOtherVar to be 10.

On the next line, I’m not declaring a new variable. We already told the program that myVar exists (and is an int). Instead, I’m assigning it on this line (re-using an existing variable). Notice that we don’t need to mention the type when the variable is used, the compiler already knows its an int. The third line adds an int to another int. This is allowed by the rules of the language (it makes sense to be able to add two whole numbers together).

Type Errors

What is the result of adding chicken soup to the concept of blue? What does the letter ‘a’ smell like? These questions doesn’t make sense2! Our mental-concept of food item doesn’t have defintion for what add means when combined with the mental-concept a of colour. Our mental-concept of letter doesn’t have an attribute smell (we’ll talk about how you might go about representing mental-concepts as code next study block).

We can end up in a similar situation in our code. What if we try to add two variables which have different types together:

Visual Studio type error

Consider the following statements:

int foo = 0;
string bar = "Hello";
foo = bar + foo - 42;

This is what it looks like in visual studio:

Code snippet above, with visual studio error highlighting

This code is not valid. On the third line, we try to store the result of adding bar (a string) to foo (an int) and 42 (a literal, of type int). The computer doens’t know how to store the string, “Hello” as an int (recall our lecture and workshop on types as for why this is the case). You could also argue it’s not meangful to add text and a whole number together (although many programming languages allow this, and will treat it as string concatination).

The only sensible thing the computer can do in this situation is to let the programmer know what we’re asking it to do doesn’t make sense (some programming languages choose to ‘have a go anyway’, we call these languages ‘weakly typed’). In this case, the error is that it doens’t make sense to do this, so we need to think about what our program is trying to do (maybe we meant to update bar and not foo).

Type Conversion

Often, we want to do things to variables of different types, for example, adding an integer to float. Often this conversion is not allowed. Many programming languages will not permit the following:

float foo = 10.0;
int bar = 15;
int baz = foo + bar;

Why not? We’re asking the computer to add two numbers together and return a result. Computers are good at adding numbers together after all.

The reason is because we could lose data, because we can’t represent floating point numbers exactly as integers (and often vice-versa). Generally, the compiler will let us perform conversions to a bigger type which won’t result in data loss (eg, we can store an int value into a long). If two types are similar enouph, we might be able to perform an explicit conversion (otherwise known as a cast) between the types. This lets the compiler know we are ok with potentially losing data.

float foo = 10.0;
int bar = 15;
int baz = (int)(foo) + bar;

  1. Some languages (like go) actually put the type after the name. ↩︎

  2. If for some reason you do have meaningful defintions of such operations, replace these examples with ones you don’t. ↩︎

Syntax Errors


Last updated 0001-01-01

Graduation Cap Book Open book GitHub Info chevron-right Sticky Note chevron-left Puzzle Piece Square Lightbulb Video Exclamation Triangle Globe