为什么Objective-C不支持方法重载?

时间:2022-12-27 21:22:15

Objective-C doesn't support methods overloading.
Why?
Is it doable but Apple decided not implement it? or it is not doable due the dynamic nature of Objective-C?

Objective-C不支持方法重载。为什么?是否可行,但Apple决定不实施它?或者由于Objective-C的动态特性,它是不可行的?

I have the impression that method overloading can be done on compiled languages (Java, C#) and can't be done on interpreted languages (Ruby, Python).
Holds some true?

我的印象是方法重载可以在编译语言(Java,C#)上完成,而不能在解释语言(Ruby,Python)上完成。有一些真实的吗?

1 个解决方案

#1


26  

The distinction that's relevant here is not between compiled and interpreted languages, but between statically typed (Java, C#) and dynamically typed (Ruby, Python, Objective-C). In a dynamically typed language, type information is very often not known until runtime. At runtime, all objects are statically typed as id in Objective-C.

这里的区别不在于编译语言和解释语言之间,而是在静态类型(Java,C#)和动态类型(Ruby,Python,Objective-C)之间。在动态类型语言中,直到运行时才会知道类型信息。在运行时,所有对象在Objective-C中静态类型为id。

Additionally, a core idea in dynamically typed OO languages is that you should not care what type an object is as long as it responds to the messages you want to send. So overloading based on type would fly right in the face of that.

此外,动态类型OO语言的核心思想是,只要它响应您要发送的消息,就不应该关心对象的类型。因此,基于类型的重载将直接面对它。

#1


26  

The distinction that's relevant here is not between compiled and interpreted languages, but between statically typed (Java, C#) and dynamically typed (Ruby, Python, Objective-C). In a dynamically typed language, type information is very often not known until runtime. At runtime, all objects are statically typed as id in Objective-C.

这里的区别不在于编译语言和解释语言之间,而是在静态类型(Java,C#)和动态类型(Ruby,Python,Objective-C)之间。在动态类型语言中,直到运行时才会知道类型信息。在运行时,所有对象在Objective-C中静态类型为id。

Additionally, a core idea in dynamically typed OO languages is that you should not care what type an object is as long as it responds to the messages you want to send. So overloading based on type would fly right in the face of that.

此外,动态类型OO语言的核心思想是,只要它响应您要发送的消息,就不应该关心对象的类型。因此,基于类型的重载将直接面对它。