Static/Dynamic typing sweet spot


Commenter TurboC linked an interesting talk about the future of programming languages. This got me thinking about something I had thought about before:

It seems that PHP has moved a bit towards static typing, and languages like C# seem to be implementing some dynamic features. Are we going towards a “mixed” language with static and dynamic typing? What’s the “sweet spot” between completely static (like C++) and completely dynamic typing (like Python)?

Static and dynamic

Both static and dynamic typing have some pros and some cons. Static typing can often lead to some complexity in applications, as you have to deal with type casting and conversion. However, it can also allow for some “security” when writing code: if you have a method, which takes an int and a string as a parameters, you can’t give it anything else. With a dynamically typed language, you could pass in anything, and it’s up to the programmer of the method to validate that the input is indeed of the correct type, and not just in the correct range.

PHP started as a dynamic language: No type hinting at all. PHP 5.something brought type hinting for classes, interfaces and arrays in functions and methods. This actually makes it easier for the programmer to create flexible code: You can define that a certain parameter must be of a specific interface or a class etc., and with this small change in syntax, you will save many lines of code that would have to perform checks that these parameters are of the correct type and throw exceptions if not and so on.

PHP’s implementation has a small flaw, though. Sometimes you may want to have an optional parameter which requires a specific class or interface, but you can’t use type hinting as there is no applicable “none” type that would be allowed with the hint. If you pass a null, you will get an error that the parameter is not of the correct type.

Now, what about C#? At 2.0, it was really more of a strictly static language. Lately, in 3.0, they’ve been introducing things like the var statement and anonymous types. While the var statement allows variable statements such as…

var hello = new List<String>();

It doesn’t actually make C# have dynamic typing: hello is still strictly a List<String> object, and you can’t assign any other kinds of types to it. Having the compiler guess the type of the variable like this is known as type inference.

The anonymous type system works similar to PHP’s arrays:

var something = new {Thing1 = "Foo", Thing2 = "Bar"};

It is interesting to see where this will eventually lead.

The sweet spot

Personally, I think dynamic typing is the way to go. However, type hinting is an excellent idea: you should be allowed to use type hinting for all types, including the builtin types like strings and ints, and for return values of functions as well.

PHP is already doing a good thing here: You have dynamic typing and you can use (limited) type hinting. I think if this was expanded, it could be great.

There’s also a concept called “duck typing” – If it walks like a duck and quacks like a duck, I would call it a duck. In practice, it means that if a variable has the required attributes/methods, it is probably of the correct type. This is a common approach in languages like Python or JavaScript, where you don’t have any kinds of type hinting. However, without a strict interface (like “foo implements Duckable”), it can be difficult to implement a correctly working object for the situation.

This is why I think having the possibility of forcing a specific, well defined, interface is a good idea. Of course, in a case where your duck-typed object doesn’t have the required methods or such, the error probably won’t slip by you. Missing methods would always cause errors from the application, unlike little variables of wrong types which could simply silently cause some unexpected behavior. Sure, you should’ve written code to check for it – but if you could simply add “int” or “string” to the function signature, wouldn’t that be simpler than writing an if block to check for the type or such?

In closing

With good, modern compilers, I think it is possible to make a well performing mixed dynamic/static language like this. There are some interesting languages springing out, such as F#, so we will hopefully see some new and old ideas put into action in new or updated languages in the future. What I personally would like to see is a language with dynaminc typing, full type hinting, and something to allow you to define which features of a duck your method needs to work correctly – a contract of sorts.