什么是动态语言,为什么 C# 不符合资格?
听播客,听说 C# 不是动态语言,而 Ruby 是。
什么是“动态语言”? 动态语言的存在是否意味着静态语言的存在?
为什么 C# 是动态语言,还有哪些其他语言是动态语言? 如果 C# 不是动态的,为什么 Microsoft 大力将其推向市场?
为什么大多数 .NET 程序员都为之疯狂并离开其他语言而转向 C#?
为什么 Ruby 是“未来的语言”?
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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
什么是动态语言?
语言是否是动态的通常是指编译器执行的绑定类型:静态或后期绑定。
静态绑定仅意味着方法(或虚拟方法的方法层次结构)在编译时绑定。 运行时可能涉及虚拟调度,但方法令牌是在编译时绑定的。 如果编译时不存在合适的方法,您将收到错误。
动态语言则相反。 他们在运行时完成工作。 它们在编译时很少或根本不检查方法是否存在,而是在运行时执行所有检查。
为什么 C# 不是动态语言?
C# 在 4.0 之前是静态绑定语言,因此不是动态语言。
为什么 Ruby 是未来的语言?
这个问题基于一个错误的前提,即确实存在一种语言,它是编程的未来。 今天还没有这样一种语言,因为没有一种语言能够最擅长完成所有需要完成的不同类型的编程。
例如,Ruby 对于许多不同的应用程序来说都是一种很棒的语言:Web 开发是一种流行的语言。 然而我不会在其中编写操作系统。
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.
在动态语言中,您可以这样做:
换句话说,类型不是静态的。 在静态类型语言中,这会导致编译器错误。
C、C++、C# 和 Java 等语言都是静态类型的。
Ruby、Python 和 Javascript 等语言都是动态类型的。
此外,这与“强类型或弱类型”不同。 那是完全不同的东西。
In a dynamic language, you can do this:
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.
我对 C# 拥抱基本集合的方式感到震惊
的编程范式。
我们可以说 C# 提供了丰富的面向对象编程,
丰富的面向组件的编程,
良好集成的功能编程,
对不同类型数据源的完整查询操作集(linq),
通过 pLinq 和并行扩展进行并行编程的优雅方法,
在下一个版本(c# 4.0)中将具有强大的动态功能,
几乎可以肯定,c# 5.0 将拥有一套可靠的元编程
特征。
可以说,c# 在集成所有这些强大功能方面做得非常出色
所有东西都集中在一个工具箱中。 我认为事情就应该如此
因为从一种编程语言跳到另一种编程语言几乎总是非常痛苦。
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# 是一种静态类型语言,因为您正在使用的每个对象的类型都需要在编译时知道。 在动态语言中,您不需要在编译时知道对象的类型。 也许您导入了一些您事先不知道的类,例如导入文件夹中的所有类,例如插件或其他东西。 或者甚至对象的类型也可能取决于用户交互。
您可以通过使用接口或基类来实现类似的效果,但它并不完全相同,因为您只能使用显式继承或实现该接口的类。
在动态类型语言中,它不关心编译时的类型是什么,它会尝试调用您按名称指定的方法,如果该方法不存在于对象上,它将抛出运行时异常,因此程序员需要确保这种情况不会发生或进行适当的处理。 您获得了灵活性,但在编译时错误检查方面损失了一些。
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.
查看维基百科条目,我们发现动态语言是一种在运行时执行以下操作的语言:大多数是在编译时执行的。 通常,在动态语言中,变量可以快速轻松地更改类型,并且通常没有单独的编译步骤(而是解释执行或真正快速的编译)。 C# 是一种更传统的语言,使用变量声明并进行编译。
维基百科条目列出了许多动态语言。
另一方面,“X 是未来的 Y”意味着有人试图向您推销某些东西。 (不一定是字面意思,而是试图以一种对说话者方便的方式影响你的信念。)
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.)
您知道 VB6 既是静态的又是动态的吗?
如果您声明具有给定类型的变量,那么您将获得静态行为:
您现在只能访问属于
Label
的 name 成员,并且智能感知知道这一点。如果您有一个类并添加
implements
关键字,那么您的类可以实现另一个类的方法。 这是VB6允许的接口继承。 您可以获得一些运行时多态性。您还可以这样声明变量:
现在智能感知不会给您任何帮助,而 VB6 将允许您使用
proxy
做任何您喜欢的事情:此行可以位于已编译和正在运行的程序中,并且不会导致任何错误。犯罪,尤其是如果它不自行运行的话。 仅当该行运行时才会进行查找。
您还可以执行:
这将运行。
是否有foo
方法并不重要。然后,任何实现了
foo
的类的任何实例都可以被赋值,并且调用的方法和 VB6 都会很高兴。请注意,随着您变得越来越动态,运行时性能会受到影响。
Did you know that VB6 is both static and dynamic?
If you declare variables with a given type, then you get static behaviour:
You can now only access members of name that are
Label
s 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:
Now intellisense doesn't give you any help and VB6 will allow you to do anything you like with
proxy
: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:
and this will run. It doesn't matter whether
<any instance>
has afoo
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.
在 C# 3.0 中,所有内容的类型都需要在编译时知道。 这是一种静态语言。 动态语言在运行时使用动态分派来决定事物的类型以及对这些事物调用哪些方法。 两种类型的语言都有其优点和缺点。 C# 4.0 将添加动态功能。 Anders Hejlsberg 在 PDC 上发表了关于静态与动态语言以及 C# 4.0 的精彩演讲。
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# 做不到这一点。
还有动态类型和动态类型。 静态类型语言。 动态类型意味着变量的类型未设置并且可以在程序执行过程中更改。
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.
静态和动态这两个词没有明确的定义。
然而,最常见的意思是两个问题:
1)在静态语言中,变量的类型(即变量可以包含或指向的值的类型)在过程中不能改变一个程序的。 例如,在 C# 中,您在定义变量时声明变量的类型,例如:
现在
a
只能保存int
值 - 如果您尝试分配字符串到它,或者调用它的方法,你会得到一个编译类型错误。2) 在静态语言中,对象的类型不能改变。 在动态语言中,对象可以更改,因为您可以附加或删除方法和属性,从而基本上将其变成完全不同的对象。
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:
Now
a
can only ever hold anint
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#是静态类型的,即int i =0; 尝试将 i 设置为字符串。 编译器会抱怨,
在 python 中,可以将用于保存整数的变量设置为保存字符串,
静态:类型是最终的,
动态:类型可以更改,
c# 正在尝试添加更多动态功能,例如 var
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
不存在真正的“未来语言”。
不同的语言有不同的目的。
您最多可以说 Ruby 是一种未来的语言。
根据维基百科:
Ruby 是一种动态语言,而 C# 不是,因为 Ruby 是解释型语言,而 C# 是编译型语言。 然而,C# 确实包含一些使其显得动态的功能。
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:
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 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.