I am trying to understand what it means for a language to be type-safe. In a dynamically typed language, the type checking is performed at run time, so for example if I run the following PHP code:
<?php
class MyClass
{
}
// Create a MyClass instance
$mc = new MyClass();
// Create an int variable
$i = 1234;
// Add $mc and $i
$result = $mc + $i;
?>
I will get an error because the + operator
does not support the MyClass
data type. So basically the type checking was performed at run time.
Does type safety means that type checking is performed regardless if it is performed at compile time or at run time, or does it mean that type checking must be performed only at compile time and so each variable must be given a data type explicitly (like C, Pascal, Java, etc.).
function x() { $i = 0; if (true) { $i = "bla"; } return $i; }
... – Royal Bgif()
block – Royal BgBigDecimal.divide
,NullPointerException
, ...) – Elias Van Ootegem