|Type Systems Demystified|
|Written by Nikos Vaggalis|
|Friday, 19 February 2010|
Page 2 of 6
In dynamic typing, or late binding, the type of an object or expression is not known until run-time. The variable is "untyped" and this is notated differently among languages. In C# and VB.net the variable is of type Object, in classic VB of type Variant and in Perl there is no type information prepended to the variable.
In dynamic typing the variable can hold a value of any type and subsequently the value and its type can change during runtime. It is not a type-safe affair since type checking cannot be done at compile time and the object must be interrogated at runtime for its members. Because of this some run-time errors cannot be avoided and it also costs in terms of performance. Another disadvantage is that you do not have access to some IDE features such as Intellisense support since there is no type library to import (borrowing the term from the COM+ world).
This example both in VB.net and C# would resolve to a compile time error since there is no such method MyMethod belonging to type Object, since static typing checks the compile time type (Object) and not the runtime type (Class1). Thus VB.net and C# display their static language properties.
The same concept holds true even if the method does not exist at all :
Again, this example both in VB.net and C# would resolve to a compile time error since there is no such method MyMethodWhichDoesNotExist belonging to either type Object or Class.
However, by using the 'option strict off' compiler directive, VB.NET acts as a dynamically typed language and type checking is deferred until runtime; subsequently it will not complain at compile time that it cannot resolve the binding to a method called MyMethodWhichDoesNotExist. Now the operation is directed by the runtime data and the object's type is asked for the method by using the method name as a string. If it does exist then the method call gets bound, if not a runtime exception is thrown.
Not real late-binding
Of course since you have Class1 type information ahead at compile time the above example has no particular use; however it demonstrates the late binding concept. Also, since you know the type at compile time you could just employ casting from type Object to the according type:
So really no late-binding magic is taking place here.
For true late -binding think of the COM+ example CreateObject with no type library where the compiler has no knowledge of the type at compile time and thus no chance the compiler knowing the members of the class ahead, since the type is constructed at runtime:
The same example with early binding , where you have type library information (compile time information on the type) and thus can declare the type of the variable, would be :
Dim myobj As Excel.Application
The term late binding is used in static languages as a synonym for polymorphism. A method denoted with the 'virtual' keyword cannot be bound to the variable at compile time; it can only be determined by the actual object type at runtime. This means that when calling a 'virtual' method by using a variable of the base type that points to an instance of the derived type, the method to be called must be determined at runtime. However the base class MUST support the method and this is enforced at compile time.
class Class2 : Class1
int x = objA.Method1();
In this example, the compile time type of objA is consulted and Method1 is looked up which does exist for Class1. Since the method is denoted as 'virtual' the next step is to consult the runtime type (which is Class2) and follow that type's method entry in its Metadata table. The method address is retrieved and bound to the variable. Again, as stated above this is not real late binding since the method must be known at compile time.
|Last Updated ( Friday, 09 November 2012 )|