What is a dynamic language, and why doesn't C# qualify?

31,865

Solution 1

What is a dynamic language?

Whether or not a language is dynamic typically refers to the type of binding the compiler does: static or late binding.

Static binding simply means that the method (or method hierarchy for virtual methods) is bound at compile time. There may be a virtual dispatch involved at runtime but the method token is bound at compile time. If a suitable method does not exist at compile time you will receive an error.

Dynamic languages are the opposite. They do their work at runtime. They do little or no checking for the existence of methods at compile time but instead do it all at runtime.

Why is C# not a dynamic language?

C#, prior to 4.0, is a statically bound language and hence is not a dynamic language.

Why is Ruby the language of the future?

This question is based on a false premise, namely that there does exist one language that is the future of programming. There isn't such a language today because no single language is the best at doing all the different types of programming that need to be done.

For instance Ruby is a great language for a lot of different applications: web development is a popular one. I would not however write an operating system in it.

Solution 2

In a dynamic language, you can do this:

var something = 1;
something = "Foo";
something = {"Something", 5.5};

In other words, the type is not static. In a statically typed language, this would result in a compiler error.

Languages such as C, C++, C#, and Java are statically typed.

Languages such as Ruby, Python, and Javascript are dynamically typed.

Also, this is not the same as "strongly or weakly" typed. That is something different all together.

Solution 3

Looking at the Wikipedia entry, we see that a dynamic language is one that does things are runtime that most do at compile time. Typically, in a dynamic language, a variable could change types quickly and easily, and there typically is no separate compile step (but rather either interpreted execution or really fast compiling). C# is a more conventional language, using variable declarations and being compiled.

The Wikipedia entry lists numerous dynamic languages.

"X is the Y of the future", on the other hand, means that somebody's trying to sell you something. (Not necessarily literally, but trying to influence your beliefs in a way convenient to the speaker.)

Solution 4

C# is a statically typed language, because the type of every object you're working with needs to be known at compile time. In a dynamic language you don't need to know what type an object is at compile time. Maybe you import some classes that you don't know before hand, like you import all classes in a folder, like plugins or something. Or maybe even the type of an object depends on user-interaction.

You can achieve a similar effect by using interfaces or base classes, but it's not completely the same because you are limited to using classes that explicitly inherit from or implement that interface.

In dynamically typed languages it doesn't care what the type is when you compile it, it'll try to call the method you specified by name, if that method doesn't exist on the object it'll throw a run-time exception, so it's up to the programmer to ensure that that doesn't happen or handle it appropriately. You gain flexibility, but lose out a little on compile-time error checking.

Solution 5

Did you know that VB6 is both static and dynamic?

If you declare variables with a given type, then you get static behaviour:

Dim name as Label

You can now only access members of name that are Labels and intellisense knows that.

If you have a class and add the implements keyword, then your class can implement methods of another class. This is inheritance of interface that VB6 allows. You can get some runtime polymorphism.

You can also declare variables like this:

Dim proxy As Object

Now intellisense doesn't give you any help and VB6 will allow you to do anything you like with proxy:

proxy.foo()

This line can sit inside a compiled and running program and cause no offence, especially if its not run itself. Its only when the line is run does the lookup take place.

You can also perform:

set proxy = <any instance>

and this will run. It doesn't matter whether <any instance> has a foo method or not.

And then any instance of any class that does implement foo can be assigned and the method called and VB6 will be happy.

Note that there are run-time performance penalties as you become increasingly dynamic.

Share:
31,865
egyamado
Author by

egyamado

Updated on December 03, 2020

Comments

  • egyamado
    egyamado over 3 years

    Listening to a podcast, I heard that C# is not dynamic language while Ruby is.

    What is a "dynamic language"? Does the existence of dynamic languages imply that there are static languages?

    Why is C# a dynamic language and what other languages are dynamic? If C# is not dynamic, why is Microsoft pushing it strongly to the market?

    As well why most of .NET programmers are going crazy over it and leaving other languages and moving to C#?

    Why is Ruby "the language of the future"?