编程语言如何既是静态类型又是动态类型?

发布于 2024-08-28 22:52:50 字数 104 浏览 7 评论 0原文

静态类型语言和动态类型语言原则上看起来是相反的概念。然而,像 Objective-C 这样的语言怎么能同时兼具这两者呢?在我看来,Objective-C 静态多于动态。有人可以解释这怎么可能吗?

Statically-typed languages and dynamically-typed languages in principle seem like opposite concepts. However, how can a language like Objective-C for example be both of these things at once? It seems to me that Objective-C is more static than dynamic. Can somebody explain how this is possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风吹过旳痕迹 2024-09-04 22:52:51

静态类型语言和动态类型语言确实是相反的——至少在这些术语的通常使用方式上是这样。

静态类型语言是一种在编译期间(或至少在代码执行之前)包含类型检查(和报告)阶段的语言。 Objective C 当然有这个,所以是静态类型的。

动态类型语言是缺乏这种类型检查阶段的语言。因此,根据这个定义,Objective C 并不是动态类型的——而且我认为这是最标准的定义。

然而,Objective C 有时被称为动态类型(除了静态类型之外),因为它允许程序员在其代码部分中指定减少的静态类型检查 - 特别是对于通过特殊静态类型 id 的对象。就我个人而言,我认为说它是一种对动态类型对象有良好支持的静态类型语言不会那么令人困惑。

请注意,几乎所有静态类型语言都包含对动态类型的一些支持,因为例如,它们使用运行时检查来捕获除零错误、数组边界错误、向下转换错误等。这些事情并不能证明动态调用语言是合理的类型化,否则几乎所有语言都必须称为动态类型化。

Statically-typed languages and dynamically-typed languages are indeed opposites - at least in the way these terms are normally used.

A statically-typed language is one that includes a type checking (and reporting) phase during compilation (or at least prior to code execution). Objective C certainly has this, so is statically typed.

A dynamically-typed language is one lacking such a type checking phase. Hence Objective C is not dynamically-typed by this definition - and I think this is the most standard definition.

However, Objective C is sometimes called dynamically-typed (in addition to statically typed) because it allows the programmer to specify reduced static type checking in parts of their code - particularly for objects via the special static type id. Personally I think it would be less confusing to say that it is a statically-typed language with good support for dynamically-typed objects.

Note that nearly all statically-typed languages include some support for dynamic typing, since e.g., they use run-time checks to catch division by zero errors, array bounds errors, downcast errors, etc. Such things don't justify calling a language dynamically typed, otherwise nearly all languages would have to be called dynamically typed.

巾帼英雄 2024-09-04 22:52:50

我相信您混淆了静态类型和动态方法解析。 Objective-C 绝对是强静态类型的。与 C 一样,所有变量都必须声明并类型化(甚至没有像其他现代静态类型语言那样的类型推断)。编译器根据变量的类型生成代码,并且该类型在运行时无法更改。

然而,Objective-C 方法调用使用消息传递范例,其中消息名称和目标在编译时编码,但要执行的代码的地址由 Objective-C 运行时库在运行时查找。

I believe you are confusing static typing and dynamic method resolution. Objective-C is definitely strongly, statically typed. Like C, all variables must be declared and typed (there isn't even type inference as in other modern, statically typed languages). The compiler generates code based on the type of variables and this type cannot be changed at runtime.

However, Objective-C method calls use a message passing paradigm, where the message name and target are encoded at compile time, but the address of the code to execute is looked up at runtime by the Objective-C runtime libraries.

清君侧 2024-09-04 22:52:50

Objective-C 实际上(概念上)只是 C 语言本身的一层,因此可以具有静态和动态类型。如果您使用的是 Base-C 内容,则为静态;如果您使用的是 Objective-C 扩展,则为动态。

但 C 也提供了这种功能。如果您只考虑 C 中的 void * 类型,您会发现它可以指向任何类型,从而为您提供了一种(非常粗糙的)动态类型语言。

例如:

int i;
float f;
double d;
void *p = &i;
p = &f;
p = &d;

在上面所有对 p 的赋值中,它都指向不同的类型。如果您的代码足够巧妙,您甚至可以在 C 中模拟 RTTI 和多态性。

我会根据使用的内容来考虑一种主要静态或动态类型的语言为了。

Objective-C is really (conceptually) just a layer on the C language itself and, as such, can have both static and dynamic types. Static if you're using the base-C stuff, dynamic if you're using the Objective-C extensions.

But C also sort-of provides this feature. If you just think about the void * type in C, you'll see that it can point to any type, hence giving you a (very rough) dynamically-typed language.

For example:

int i;
float f;
double d;
void *p = &i;
p = &f;
p = &d;

At all those assignments to p above, it's made to point to a different type. If you do your code cleverly enough, you can even emulate RTTI and polymorphism in C.

I would consider a language primarily statically or dynamically typed, based on what it was most used for.

飘然心甜 2024-09-04 22:52:50

C 是一种静态类型语言,但它可以灵活地将类型重新转换为其他类型,并可以使用泛型指针(void* 类型)。 void* 类型的意思是“指向未指定数据类型的指针”。 Objective-C 通过使用这些 void* 类型来实现其动态类型,尽管通常这是通过多层定义、typedef 等抽象出来的。

C is a statically-typed language, but it has the flexibility to re-cast types to other types, and to use generic pointers (the void* type). The void* type means "a pointer to an unspecified type of data". Objective-C implements its dynamic types through the use of these void* types, though usually this is abstracted away by multiple levels of defines, typedefs, etc.

筱果果 2024-09-04 22:52:50

Objective-C 混合了静态类型和动态类型。普通 C 对象是静态类型的,但 Objective-C 对象是动态类型的。 Objective-C 运行时并不关心对象是什么类型,只要对象能够识别您发送给对象的消息即可。

Objective-C has a mixture of static and dynamic typing. The plain C objects are statically typed, but the Objective-C objects are dynamically typed. The Objective-C runtime does not care what type an object is as long as the messages you send to your objects are recognised by the object.

疏忽 2024-09-04 22:52:50

如果您询问支持这两种习惯用法的技术能力,那么这不是一个特别有趣的问题。只要看看大多数现代语言,看看它们是如何做到的。通常,它是通过某种包罗万象的动态类型(VB 中的 Object、C# 中的 dynamic、C 中的 void* 等等)。

在形而上学/本体论层面,这个问题更有趣……

物理学理论可能会提出违背直觉的行为,导致人们问:“那怎么可能?”例如,波粒二象性超越了现实如何或应该如何的任何常识性概念,因此它让我们思考不可估量的问题。

然而,编程语言并不是现实的模型。它们是心灵的发明,旨在服务于我们的目的。因此,思考一种编程语言是如何变成现在这个样子是没有意义的。之所以是这样,是因为我们希望它是那样;因为它符合我们的目的。没有其他理由是必要或保证的。

因此,请理解,当我回答一种语言既可以是动态的也可以是静态的时,我并不是轻率或轻蔑,只是因为它可以,并且因为这很有用。试图进一步探究是没有希望的。

If you are asking about the technical ability to support both idioms, that's not a particularly interesting question. Just look at most modern languages and see how they do it. Usually, it's via some kind of catch-all dynamic type (Object in VB, dynamic in C#, void* in C, and so on).

At the metaphysical/ontological level, this question is much more interesting...

A theory in physics might suggest behaviour that defies intuition, leading one to ask, "How can that be?" For instance, the wave-particle duality goes beyond any commonsensical notion of how reality is or ought to be, and so it leaves us pondering the imponderable.

Programming languages, however, are not models of reality. They are inventions of the mind, designed to serve our purposes. It is thus meaningless to ponder how a programming language can be the way it is. It is that way because we wanted it to be that way; because it suits our purposes. No other reason is necessary or warranted.

So please understand that I am not being flippant or dismissive when I answer that a language can be both dynamic and static just because it can, and because this is useful. It is hopeless to try to probe any further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文