Does the type safety of a programming language implement its binding mechanism or vice versa?

I am really confused about the concept of Strongly typed programming language (AKA type safety enforcement). Based on multiple definitions from Wikipedia source I can conclude from how I understood it that Type safety is really about what can and cannot be done with a language as predicted by the language itself (correct me if I am wrong)..

This made me remember glancing through an article that talked about binding. It got me all confused as to if there is any relationship between static/dynamic type safey assertion and static/dynamic binding of fields..

For example an Integer variable int two = 1; is statictly binded to its value, trying to assign a string value “1” to integer an variable will result in a compile time error like int one ="1";. I consider this a type safety approach implemented by static binding. Am I wrong thinking this way? Please enlighten my understanding of this concept.

4

No. Type system and binding are distinct concepts.

The type system as the name says has to do on who and how the types themselves are checked. The binding has to do with the link between the data or code/operation and the names (of procedure, variable). Related to binding is also the term of dispatch, which has to do with how/when it’s decided which operation is performed. It happens that binding is confused with dispatch.

Let’s take as example Java:

Type system is:

  • static: you must specify types when you declare them so the compiler checks
  • strong: compiler checks the types of the operations and don’t allow if not matching

Binding can be of both types:

  • static: in static functions using static fields
  • dynamic: when using Reflection or when having dynamic dispatch (see below)

Dispatch can be of both types:

  • early: when using calling static methods
  • dynamic: when calling virtual methods, allowing polymorphism

Taking C as example:

Type system:

  • static: you must define the types when you use them
  • weak: you can add an int to char for example

Binding:

  • static: the compiler does all the stuff

Dispatch:

  • static: as above, it’s decided at compile time what functions are called

2

I think it is more about declaring the type vs. run-time type resolving.
And of course being able to change bound variables’ types after initialization. For example c#’s Implicitly Typed Local Variables doesn’t change its static typed checking feature. But if you use dynamic it is totally different. Compiler can’t warn you when using dynamic keyword.

To summarize, it is the explicit declaration of types and static analyze of the compiler.

You can examine Typescript if you want how static type checking implemented in a non type checking language: JavaScript.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *