Dynamic Typing

In my last post, I talked about Duck Typing and I said it is one of the principle of Dynamic Typing, but don’t have to be in a dynamically typed language.

In order to discuss Dynamic Typing, we need to go thorough other typing systems and know the differences between them

Implicit Typing (or Type Inference) [C# 3.0 and later]:

It is one of the forms of Latent Typing as well as Duck Typing and Dynamic Typing. Let’s see this example

var test = 5; // test is an integer

Here, you don’t have to specify the type as the Compiler can infer from the statement that the variable will hold an integer. That’s why it cannot be used if declaration and assignment are not on the same statement…

var test; // Compilation error
test = 5;

Implicit typing is used mainly when the developer wants to use the result of a function, or an expression (as in LINQ) without the need to specify its type, as the compiler already knows the type. Also, if Explicit casting is used, there is no need for explicit typing…

var test = (int) cmd.ExecuteScalar();

Dynamic Typing [Ruby, Python, and C# 4.0]

It is another form of Latent Typing, but unlike Implicit typing, type is deduced at runtime, so, there is no type checking at compile time. The support for dynamic typing is introduced in C# 4.0 to support API calls into dynamic type languages, or the use of other non-typed data, like XML.

XElement element = XElement.Parse(@"
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
    </Person>");
dynamic personXml = new DynamicXml(element);
personXml.FirstName = "Bob";

It is important to know that once a variable type is defined at runtime, it cannot be redefined later as another type. i.e. Dynamic Typing is not Weak Typing

Weak Typing (or Loose Typing) [Javascript, VB 6.0]

In javascript, this code is valid

var x = new Array(); // Any array
x[0] = "One"; // First element is a string, other elements can be any type
x[1] = "two";
x[2] = "three";
alert(x.length); // return 3
x = x.join(""); // Now x is a string not an Array
alert(x.length); // now return 11 (string length)

Weak typing is antonym to Strong Typing, while Dynamic Typing is antonym to Static Typing.

Here are some good related articles about typing systems by Bruce Eckel (author of Thinking in C++ and Thinking in Java):

Strong Typing vs. Strong Testing
About Latent Typing
How to Argue about Typing
I’m Over It

Advertisements

Duck Typing

While browsing the net, I found that Typemock Isolator 5.1.1 has been released, which is a mocking framework that I should try one day.

What I’m interested in is its ability to “swap” calls between real and fake objects even if they don’t implement the same interface, like this:

var duck = new Duck(); // The real, untestable class, with a Talk() method
var dummy = new Dummy(); // The fake, testable class, with another Talk() method
Isolate.Swap.CallsOn(duck).WithCallsTo(dummy);
Assert.AreEqual("Quack", dummy.Talk()); // Then the dummy is a duck

I guess the Isolate class is “borrowed” from Ruby, but anyway, it makes testing easier.

This can be possible by applying the concept of “Duck Typing” which was named as James Whitcomb Riley said


If it walks like a duck and quacks like a duck, I would call it a duck

 

So, regardless of the properties and methods of both the dummy and the real duck, a dummy can be considered a duck if it satisfies the properties and methods of a real duck in the current context, or in other words; it does not have to inherit from a duck nor satisfy all behavior of a real duck, but only what we need right now [Talk()]

Duck typing is a principle of Dynamic Typing (Which I plan to blog about next), and it is a heavily used in Dynamic Languages like Python, Ruby.

It is sometimes used in Static Languages, and may be more often than we expect. Here is a blog about Duck Notation in C# in which we find out that iterators in a foreach statement use duck typing. Also in BOO you can define a variable as duck.

Currently there is an open source project to enable duck-typing in C#, and also it is planned to be in the upcoming C# 4.0