Question

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"?

Was it helpful?

Solution

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.

OTHER TIPS

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.

I'm stunning at the way c# it's embracing a fundamental set of programming paradigms.

We can say that c# alows a rich object oriented programming, a rich component oriented programming, a well integrated functional programing, a complet set of query operations over differents types of data sources (linq), a elegant aproach of cocurrent programming through pLinq and parallel extensions, in the next release (c# 4.0) will have powerfull dynamic capabilities, and it's almost sure that in c# 5.0 will have a solid set of meta-programming features.

With just can say that c# it's doing a great job of integrating all this powerfull stuff in just one tool box. That's in my opinion it's the way it must be, because skipping from one programming language to another it's almost always very painfull.

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.

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.)

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.

In C# 3.0, the types of everything needs to be known at compile-time. It's a static language. A dynamic language uses dynamic dispatch at runtime to decide the type of things and what methods to call on those things. Both types of languages have their advantages and disadvantages. C# 4.0 will add dynamic capability. Anders Hejlsberg gave a great talk on static v.s. dynamic languages and C# 4.0 at PDC.

The words static and dynamic are not cleary defined.

However, what is most often meant is two issues:

1) In static languages, the type of a variable (that is, the type of value the variable can contain or point to) cannot change during the course of a program. For example in C#, you declare the type of a variable when you define it, like:

int a;

Now a can only ever hold an int value - if you try to assign a string to it, or call a method on it, you will get a compile type error.

2) In static language the type of an object cannot change. In dynamic languages, an object can change in that you can attach or remove methods and properties, thereby basically turning it into a completely different object.

c# is statically typed, ie int i =0; try setting i to be a string. the compiler will complain,

where as python a variable that used to hold an integer can then be set to hold a string,

Static: Types are final, Dynamic: Types can be changed,

c# is trying to add more dynamic like features, var for instance

There is no true "language of the future". Different languages have different purposes. At most, you could say Ruby is a language of the future.

According to Wikipedia:

Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them. Most dynamic languages are dynamically typed, but not all.

Ruby is a dynamic language and C# is not, since Ruby is interpreted and C# is compiled. However, C# does include some features that make it appear dynamic.

A dynamic language is generally considered to be one that can dynamically interpret & generate code at runtime. C# can't do that.

There are also dynamically typed & statically typed languages. Dynamically typed means that the type of a variable is not set and can change throughout the program execution.

A language is dynamically typed, that means a variable in that can be used for anything. Variables are not typed, values are. A variable can have the value of any primitive type, or it can reference to any object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top