Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because compiled languages need to take the amount of memory used in account when they are compiled.</p> <p>When you see something like:</p> <pre><code>int a </code></pre> <p>in C++, the compiler spits out code that reserves four bytes of memory and then assigns the local symbol "a" to point to that memory. If you had a typeless scripting language like javascript, the interpreter, behind the scenes, allocates the memory required. You can do:</p> <pre><code>var a = 10; // a is probably a four byte int here a = "hello world"; // now a is a 12 byte char array </code></pre> <p>There is a lot that happens between those two lines. The interpreter deletes the memory at a, allocates the new buffer for the chars, then assigns the a var to point to that new memory. In a strongly typed language, there is no interpreter that manages that for you and thus the compiler must write instructions that take into account type.</p> <pre><code>int a = 10; // we now have four bytes on the stack. a = "hello world"; // wtf? we cant push 12 bytes into a four byte variable! Throw an error! </code></pre> <p>So the compilers stops that code from compiling so the CPU dosn't blindly write 12 bytes into a four byte buffer and cause misery.</p> <p>The added overhead for a compiler writing extra instructions to take care of type would slow down the language significantly and remove the benefit of languages like C++.</p> <p>:)</p> <p>-nelson</p> <p><strong>EDIT in response to comment</strong></p> <p>I don't know much about Python, so I can't say much about that. But loosely typedness slows down runtime considerably. Each instruction that the interpreter (VM) calls has to evaulate, and if necessary, coerce the var into the expected type. If you have:</p> <pre><code>mov a, 10 mov b, "34" div a, b </code></pre> <p>Then the interpreter has to make sure that a is a variable and a number, then it would have to coerce b into a number before processing the instruction. Add that overhead for every instruction that the VM executes and you have a mess on your hands :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload