Binding a static type to a binding to a dynamic type

I understand what is meant by type binding. Please correct me if I am wrong -

Type binding is the process of "binding" a declared variable to a specific type
(Done by the compiler).

Type binding can be classified as -
Static Type Binding
Dynamic type binding

Static type binding can be achieved by two types of declarations -

http://sankofa.loc.edu/CHU/WEB/Courses/Cosi350/Ch4/bound.2.gif

This is still clear.
But now, what is binding to a dynamic type? (not a definition) I know that this means that the variable is associated with the type at runtime,

http://sankofa.loc.edu/CHU/WEB/Courses/Cosi350/Ch4/bound.3.gif

More information about this, for example -

  • Why is binding a dynamic type?
  • In what programming languages ​​is it available as a function?
  • What are its advantages and disadvantages compared to binding to a static type?

Specified via assignment operator

Can you give more information about this. Should only an assignment operator be specified?

+6
source share
1 answer

I will try to answer your questions:

First the easiest. In which programming languages ​​is it available as a function?

Php, Python, Ruby, Perl, JavaScript ...

"What is a dynamic type binding?"

Programming languages ​​that use this approach to bind a variable type, such as PHP, Python, Ruby, etc., are mostly interpreted. So they do not have a compiler. In these languages, you do not specify the type of the variable and do not have rules of nonstatality for this, therefore it is "impossible" to detect the type of the variable before starting.

Why use a dynamic type binding?

To answer this question, I have to talk about some of the benefits of this approach. To choose whether to use Dynamic Type Binding is the decision of your language. So, it depends on what you want for your language and in what situation it will be used.

<strong> Benefits

  • It’s easier to write common code.

Example: Consider creating a common data structure such as Stack. You want to use it to store all variables using different stacks, for example, a stack for int, another for a string, and so on. For this, in programming languages ​​** with binding to a static type ** this is more complicated.

In C, for example, you have two options:

  • Write one stack for each type you want to use on the stack
  • Create a stack that saves (void *), in other words, a point for something

If you try to do this in Python, you write your code right away. Language does the hard work for you.

You can argue about Java and other languages, but you should keep in mind that dynamic type binding is a concept older than oriented programming of objects, so there was no polymorphism at that time.

disadvantages

  • High cost of type checking and interpretation

Running a program using the binary generated by compilation is much faster than the same program as on the interpreter. Secondly, when you use a static type binding , "basically, the binding process is performed only once, before execution. On the other hand, in languages ​​with a dynamic type binding, the binding process is performed for each assignment operation to each variable.

Other minimal flaws are some errors that the compiler could find, but using a dynamic type binding , the interpreter cannot.

For example: since you can change the type of your variables as many times as you need at runtime, it is really possible to create confusion, for example, change an int variable to a string, and after some point try to call a function that receives an int, and you call this function using a variable that is now a string.

This is a minimal problem, because the developer-developer will never do this, but it is still possible, and there is no static type in languages ​​with a binding .

You should keep in mind that we are talking about concepts, and the implementation of these concepts is being developed along with computer science, so perhaps some advantages / disadvantages can be minimized these days.

+16
source

Source: https://habr.com/ru/post/911491/


All Articles