2

I am trying to understand static vs dynamic typing, but am really struggling to see how everything fits together.

It all starts with data types. As far as I understand, data types are quite abstract notions, which exist 'in' compilers in order to categorise data so that the operations on various types of data can be validated (i.e. in an attempt to stop you adding a string to an integer), and in order to generate the right machine code for the hardware interpretation of the value. I.e. say we have the following:

int myInt = 5;
char myChar = '5';

Console.WriteLine(myInt);
Console.WriteLine(myChar);

Both would ultimately write a five to the console window, but, since the representations in memory of integers and characters are different, the machine code which interprets the value in the memory location bound to the variable myInt, which takes that value and displays it on the console window, would be different to the machine code for myChar. Even though Console.WriteLine() 'does the same job', the different representations of five require different low level code.

So my question is this: if data types 'exist only in the compiler' - i.e. once the program has been compiled into machine code there is no knowledge of what type of data the value in a particular memory cell is (everything is just 1s and 0s) - then how can any type-checking be done at runtime? Surely there is no concept of data types at run time? So surely dynamic typing can't be anything to do with type-checking occurring at run time?

Where is my understanding going wrong, and could somebody please explain static and dynamic typing with respect to the argument given above? What is the big picture of what is going on?

I am trying to understand this for an essay, so references to books or online sources would be useful :) Thank you.

Lord Cat
  • 401
  • 4
  • 13
  • 1
    Actually, the compiler leaves 'clues as to what 'type' something is. Be aware that the processor knows about integers and 'floating point' numbers. The compiled output is often actually calls to different sub-routines that know how to check and process the different types. Dynamic typed variables are a 'structure', that holds what the type of data is currently stored in it, this is used by the 'runtime system' to work out how to process them. – Ryan Vincent Nov 28 '15 at 17:57

1 Answers1

0

When a language requires type information at runtime¹, implementations need to simply represent values (or at least those values that require type information) in a way that stores such information. For example it is common for objects in object oriented languages to simply contain a field that stores information about their class.

¹ Which is not just the case in dynamically typed language, but also languages that are mostly statically typed yet allow querying type information at runtime (like instanceof/is and getClass/GetType in Java and C# respectively) or casts that could fail at runtime (like downcasts in OO languages).

sepp2k
  • 363,768
  • 54
  • 674
  • 675