3
votes

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.).

2
For this particular situation, there is some kind of type safety, but there's none if you say function x() { $i = 0; if (true) { $i = "bla"; } return $i; } ...Royal Bg
btw. your exmaple will result in 1235 so there's no safety there as wellRoyal Bg
@Royal Bg If a language performs type checking at run time and halt the program if a type error was found, is such a language called type safe language?user4407915
Most probably not. In some papers you might find these language as limited type safe languages or type safe in a limited context. For me, there's no type safety, if you can hide it in an if() blockRoyal Bg
C is not entirely typesafe. If it was, it wouldn't have as much undefined behavior as it does. C# is not necessarily statically, strongly typed, but it is considered typesafe. But then again, it needn't be, if you don't want it to be. Java was designed to be typesafe, but still has a series of exceptions that you have to throw/handle in case the type system lets you down (BigDecimal.divide, NullPointerException, ...)Elias Van Ootegem

2 Answers

6
votes

"Type safe" usually means "memory type safe", that is, you can't treat the memory that contains one type as if it was another type.

By this definition, most higher-level languages, including dynamically typed languages, are type safe, because any attempt to use a type incorrectly is guaranteed to cause an error (compile-time or run-time) in them.

So, type safety is mostly an issue for low-level languages, especially C and C++. And those issues often involve pointers or casting (e.g. reinterpret_cast in C++).

C# is an interesting case that lies between the two groups: it is type safe by default, but you can turn off type safety for parts of your code using the unsafe keyword (usually for performance or interop reasons).


But if you combine type safety with static typing, it doesn't necessarily mean having to write types. Many static type-safe languages, especially functional ones, or those inspired by them, use type inference. This means that the compiler can figure out the type of a variable by itself, based on what's assigned to it, so you don't have to type it. Examples of this are the auto keyword in C++ and var in C#.

3
votes

The answer is no. Python is an example of a dynamically but strongly typed language:

>>> "foo" + 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

Haxe is an example of a compile-time type-safe language, which still doesn't require types to be declared statically:

class Test {
    static function main() {
        trace(10 * Test.some());
    }

    static function some() {
        return "foo";
    }
}

> Build failure
> Test.hx:3: characters 19-30 : String should be Int

This is done with type inference.