Dynamic Typing

Dynamic Typing: the property of a language where type checks are performed mostly at run time. Traditionally (and mayhap not wisely) opposed to StaticTyping. Another term that is sometimes used is "value typed" opposed to "variable or reference typed". See the entry on ManifestTyping for an alternative set of definitions.

Note that DynamicTyping is not the same as WeakTyping, and many languages use DynamicTyping with StrongTyping (e.g. SchemeLanguage, PythonLanguage). See TypingQuadrant.

SmalltalkLanguage, PythonLanguage, RubyLanguage, LispLanguage are some examples of dynamically-typed languages.

A variable is dynamically typed when the type of the object(s) it will name is not specified at compile time. A program is dynamically typed if it uses at least one dynamically typed variable. A language supports dynamic typing if it is possible to write dynamically typed programs in it. A language is dynamically typed if most variables in typical programs written in that language are dynamically typed.

Here, "variable" should be interpreted broadly, to include things like slots in structures or class instances.

I think there are really two separate notions of dynamism here; "syntactic dynamic typing", meaning that the programmer doesn't need to write type declarations, and "semantic dynamic typing", meaning that the variable really can contain objects of different types. Type inference blurs the distinction a little. And, is the following program dynamically typed?

 foo = 123
 while true:
  x = random_positive_integer()
  y = random_positive_integer()
  z = random_positive_integer()
  n = random_positive_integer() + 2
 if x^n + y^n == z^n:
  foo = "fnord"
A sufficiently smart compiler (meaning, as smart as several of the 20th century's best number theorists put together) could prove that foo is always an integer. :-)

A program isn't dynamically typed, a language is. So the answer is no.

Type inference isn't "syntactic dynamic typing" it isn't dynamic typing at all, it isn't type checking at all. Nevertheless, in a static type inferred language, smart compiler or no, this program is ill-typed. Why would it care if it will never actually reach the last line (it certainly won't check that)? It's an error to attempt to assign a string to an integer whether it will ever actually happen or not.

Yes, it would be an error... hence an inference that the type of foo is actually Object or something appropriately general. Assuming a java-like system... what if we only care about the results from the toString() method?

hence an inference that the type of foo is actually Object or something appropriately general and then when you use it as something specific, the typechecker goes "WRONG!".

A smart compiler will inference the type of foo as union { int; string; }, so it accepts an integer or a string, but not all.

These days you can do some pretty strange stuff with C++ too. Check out http://www.linuxjournal.com/article.php?sid=3687

If I went the same place you did, (a page on dynamic self registering classes in C++) then you have not seen anything yet. Try C++ functors on a search engine, or for the really serious stuff the BlitzPlusPlus library

You can also do object factories for a given "interface" using templates. ANYONE can do dynamic typing in smalltalk or one of those languages which was designed for it. It takes a RealProgrammer to pull it off in c++ ;-)

Aww, Gee thanks terribly, no one has said anything so nice to me in ages.

By the way, have you ever looked at ObjectiveCee? Nifty language, though you can do most of the same stuff in C++ these days.


I have done some template magic to be able to dynamically load "function objects" with arbitrary C++ types on input and output arguments, and my conclusion is: while it can be done, it's simpler and even more efficient to do this kind of thing in, say, Scheme. Otherwise you're fighting the system every step of the way. -- StephanHouben

Otherwise you're fighting the system every step of the way.

In CeePlusPlus (and its imitators), you're supposed to fight the system every step of the way. This is called "JobSecurity", for programmers (not that it's working very well). It's a darwinian thing -- the nearly impossible task of making the "language" do what you want it to do forces you to become so focused on the one thing you're trying to accomplish that all else falls by the wayside. Perhaps RefactoringByResistance captures what I mean.

If you feel the need to fight the language when programming in CeePlusPlus, you probably haven't understood how to use it properly, and are trying to use it in a way it's not designed. A good language/library should be easy to use correctly, and hard to use incorrectly. Such is also the design of CeePlusPlus. If you're fighting it, you're working against the language, and should either learn it properly, or use another, if another suits you better. Personally, I think static typing is great for catching fundamental type errors, at compile time, rather than when the code is in use by customers...

I'm glad to hear I'm not the only one who feels that programming in CeePlusPlus is like wading through molasses. BTW, someone should start a TemplatesSmell page if they haven't already... --ChrisHandley

[But templates (generics) aren't evil. Certainly, they are unnecessary in a dynamically-typed language; but they are quite useful in a statically-typed language. Now, there are issues with the C++ implementation; but generics themselves aren't evil at all.]

[Templates can be a great thing. The problem, I find, is that a whole Turing complete language was inadvertently created with the C++ type system, and now some people (notably the Boosters from boost.org) extensively use this feature. I'd rather have my program run slower due to virtual calls than do the perverted things they do - my head just cannot handle the syntax.]

SmalltalkLanguage, PythonLanguage, RubyLanguage, LispLanguage are some examples of dynamically-typed languages.

A variable is dynamically typed when the type of the object(s) it will name is not specified at compile time.

Regarding the two above statements, I'm not sure that RubyLanguage fits the definition of a dynamically typed language, because every variable is the same type -- reference to object. Perhaps it's one of those Zen things, where it's both statically typed and dynamically typed at the same time. :-)

All of the above named languages share that trait, which you could call 'implicit reference'. The only dynamically typed language that I can think of that doesn't have implicit references is Perl, where if you want a reference, you gotta request it explicitly with the \ operator.

--I just added a rant about this at the bottom in FirstClassTypes. In this languages, variables have no types, but try to add two strings in Scheme! => ERROR! Values DO have type. Is this distinction so difficult, or everyone is a C programmer? (In C, VARIABLES have type, but VALUES don't, that's how type casting works)--

{Each language (author) can define types to be whatever the hell they want for that particular language (within a ball-park of common fuzzy notions). However, that doesn't mean there is a universal consensus that cuts across all possible languages. It's almost comparable to DefinitionOfLife.}

I'm not sure really, but I think UnrealScript? might be a DynamicTyping language simply because the following code would work if I bothered to do the other parts right:

 Actor A;
 foreach AllActors(class'Actor',A,"Groupoflights")
This code would work if all the objects in the group Groupoflights were lights, which may exhibit DynamicTyping because Actor has no LightColor?. I think that i would even work without error if other Actors were in the group, and for those actors the code would silently fail.

But I'm just a moron.


See: BenefitsOfDynamicTyping, DynamicTypingExampleCode, TypelessVsDynamic


EditText of this page (last edited March 4, 2014) or FindPage with title or text search