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.