什么是动态语言,为什么 C# 不符合条件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/787239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a dynamic language, and why doesn't C# qualify?
提问by egyamado
Listening to a podcast, I heard that C# is not dynamic language while Ruby is.
听一个播客,我听说 C# 不是动态语言,而 Ruby 是。
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 notdynamic, why is Microsoft pushing it strongly to the market?
为什么 C# 是一种动态语言,而其他哪些语言是动态的?如果 C#不是动态的,为什么微软要大力推向市场?
As well why most of .NET programmers are going crazy over it and leaving other languages and moving to C#?
以及为什么大多数 .NET 程序员都为它疯狂并离开其他语言并转向 C#?
Why is Ruby "the language of the future"?
为什么 Ruby 是“未来的语言”?
采纳答案by JaredPar
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# 不是动态语言?
C#, prior to 4.0, is a statically bound language and hence is not a dynamic language.
4.0 之前的 C# 是静态绑定语言,因此不是动态语言。
Why is Ruby the language of the future?
为什么 Ruby 是未来的语言?
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.
例如,Ruby 是一种适用于许多不同应用程序的出色语言:Web 开发是一种流行的语言。但是,我不会在其中编写操作系统。
回答by Brian Genisio
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.
C、C++、C# 和 Java 等语言是静态类型的。
Languages such as Ruby, Python, and Javascript are dynamically typed.
Ruby、Python 和 Javascript 等语言是动态类型的。
Also, this is not the same as "strongly or weakly" typed. That is something different all together.
此外,这与“强或弱”类型不同。那是完全不同的东西。
回答by Fusspawn
c# is statically typed, ie int i =0; try setting i to be a string. the compiler will complain,
c# 是静态类型的,即 int i =0; 尝试将 i 设置为字符串。编译器会抱怨,
where as python a variable that used to hold an integer can then be set to hold a string,
在 python 中,用于保存整数的变量可以设置为保存字符串,
Static: Types are final, Dynamic: Types can be changed,
静态:类型是最终的,动态:类型可以改变,
c# is trying to add more dynamic like features, var for instance
c# 正在尝试添加更多类似动态的功能,例如 var
回答by JacquesB
The words staticand dynamicare 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:
1)在静态语言中,变量的类型(即变量可以包含或指向的值的类型)在程序运行过程中不能改变。例如在 C# 中,您在定义变量时声明它的类型,例如:
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.
现在a
只能保存一个int
值——如果你试图给它分配一个字符串,或者调用它的方,你会得到一个编译类型错误。
2) In static language the type of an objectcannot 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.
2)在静态语言中,对象的类型不能改变。在动态语言中,一个对象可以改变,因为你可以附加或删除方和属性,从而基本上将它变成一个完全不同的对象。
回答by JP Alioto
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.
在 C# 3.0 中,所有东西的类型都需要在编译时知道。它是一种静态语言。动态语言在运行时使用动态调度来决定事物的类型以及调用这些事物的方。两种类型的语言都有其优点和缺点。C# 4.0 将添加动态功能。 Anders Hejlsberg 在 PDC 上发表了关于静态与动态语言以及 C# 4.0 的精彩演讲。
回答by luiscubal
There is no true "language of the future". Different languages have different purposes. At most, you could say Ruby is alanguage of the future.
没有真正的“未来语言”。不同的语言有不同的目的。最多,你可以说 Ruby 是一种未来的语言。
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.
Ruby 是一种动态语言,而 C# 不是,因为 Ruby 是解释型的,而 C# 是编译型的。但是,C# 确实包含一些使其看起来动态的功能。
回答by Steven
A dynamic language is generally considered to be one that can dynamically interpret & generate code at runtime. C# can't do that.
动态语言通常被认为是一种可以在运行时动态解释和生成代码的语言。C# 不能这样做。
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.
还有动态类型和静态类型语言。动态类型意味着变量的类型没有设置并且可以在整个程序执行过程中改变。
回答by Davy8
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.
C# 是一种静态类型语言,因为您正在使用的每个对象的类型都需要在编译时知道。在动态语言中,您不需要在编译时知道对象的类型。也许你导入了一些你事先不知道的类,比如你导入一个文件夹中的所有类,比如插件什么的。或者甚至对象的类型取决于用户交互。
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.
在动态类型语言中,它不关心编译时的类型是什么,它会尝试调用您按名称指定的方,如果该方不存在于对象中,它将抛出运行时异常,因此由程序员来确保不会发生这种情况或适当地处理它。您获得了灵活性,但在编译时错误检查方面损失了一些。
回答by David Thornley
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.
查看Wikipedia 条目,我们看到动态语言是一种在运行时执行大多数在编译时执行的操作的语言。通常,在动态语言中,变量可以快速轻松地更改类型,并且通常没有单独的编译步骤(而是解释执行或非常快速的编译)。C# 是一种更传统的语言,使用变量声明并进行编译。
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.)
“X 是未来的 Y”,另一方面,意味着有人试图卖给你一些东西。(不一定是字面上的,而是试图以一种对说话者来说方便的方式影响你的信念。)
回答by David Thornley
I'm stunning at the way c# it's embracing a fundamental set of programming paradigms.
我对 c# 包含一组基本编程范式的方式感到震惊。
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.
我们可以说,c# 支持丰富的面向对象编程、丰富的面向组件编程、良好集成的函数式编程、对不同类型数据源 (linq) 的完整查询操作集、通过 pLinq 和并行的优雅并发编程方扩展,在下一个版本 (c# 4.0) 中将具有强大的动态功能,并且几乎可以肯定,在 c# 5.0 中将具有一组可靠的元编程功能。
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# 在将所有这些强大的东西集成到一个工具箱中方面做得很好。在我看来,这就是它必须的方式,因为从一种编程语言跳到另一种编程语言几乎总是非常痛苦。