PowerShell 是强类型语言吗?
PowerShell 肯定属于动态语言的范畴,但它会被视为强类型吗?
PowerShell is definitely in the category of dynamic languages, but would it be considered strongly typed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这些术语存在一定程度的混乱。 本文解释了一种有用的类型分类法系统。
PowerShell 是动态、隐式类型的:
没有类型错误 - 变量可以在运行时更改其类型。 这就像 Python,Perl,JavaScript 但是与 C++、Java, C# 等。
但是:
因此,如果您愿意,它还支持显式变量输入。 但是,类型检查是在运行时而不是编译时完成的,因此它不是静态类型的。
我看到有人说 PowerShell 使用类型推断(因为您不必声明变量的类型),但我认为这是错误的说法。 类型推断是在编译时进行类型检查的系统功能(如 C# 中的“
var
”)。 PowerShell 仅在运行时检查类型,因此它可以检查实际值而不是进行推理。但是,正在进行一定量的自动类型转换:
因此某些类型会即时转换。 根据大多数定义,这将使 PowerShell 成为一种弱类型语言。 它肯定比Python(几乎?)从不动态转换类型更弱。 但可能不像 Perl 那么弱,Perl 几乎可以根据需要转换任何东西。
There is a certain amount of confusion around the terminlogy. This article explains a useful taxonomy of type systems.
PowerShell is dynamically, implicit typed:
No type errors - a variable can change its type at runtime. This is like Python, Perl, JavaScript but different from C++, Java, C#, etc.
However:
So it also supports explicit typing of variables if you want. However, the type checking is done at runtime rather than compile time, so it's not statically typed.
I have seen some say that PowerShell uses type inference (because you don't have to declare the type of a variable), but I think that is the wrong words. Type inference is a feature of systems that does type-checking at compile time (like "
var
" in C#). PowerShell only checks types at runtime, so it can check the actual value rather than do inference.However, there is some amount of automatic type-conversion going on:
So some types are converted on the fly. This will by most definitions make PowerShell a weakly typed language. It is certainly more weak than e.g. Python which (almost?) never convert types on the fly. But probably not at weak as Perl which will convert almost anything as needed.
PowerShell 是动态类型的、简单明了的。 它的创建者 Bruce Payette 如此描述它。
此外,如果有人参加过基本的编程语言理论课程,他们就会知道这一点。 仅仅因为有类型注释系统并不意味着它是强类型的。 甚至类型注释的变量在强制转换期间也会动态地表现。 任何允许您将字符串分配给变量并将其打印出来,然后将数字分配给同一变量并用它进行计算的语言都是动态类型的。
此外,PowerShell 的作用域是动态的(如果这里有人知道这意味着什么的话)。
PowerShell is dynamically typed, plain and simple. It is described as such by its creator, Bruce Payette.
Additionally, if anyone has taken a basic programming language theory class they would know this. Just because there is a type annotation system doesn't mean it is strongly typed. Even the type annotated variables behave dynamically during a cast. Any language that allows you to assign a string to a variable and print it out and then assign a number to the same variable and do calculations with it is dynamically typed.
Additionally, PowerShell is dynamically scoped (if anyone here knows what that means).
我认为进一步查看向 Int 添加 String 示例将为讨论提供更多依据。 什么被认为是动态类型转换? 其中一条评论中有人说,在这种情况下:
“4”
变成了 Int32。 我根本不相信情况是这样。 我认为,中间步骤发生在命令更改为:请注意,这意味着
"4"
在整个过程中保持字符串状态。 为了证明这一点,请考虑以下示例:I think looking at the adding a String to an Int example further would provide more grist for the discussion mill. What is considered to be dynamic type casting? Someone in one of the comments said that in this case:
The
"4"
becomes an Int32. I don't believe that is the case at all. I believe instead that an intermediate step happens where the command is changed to:Note that this means that
"4"
stays a String through the entire process. To demonstrate this, consider this example:如果您需要的话,它可以是。
像这样:
使用 [type] 表示法来指示您是否关心变量是强类型的。
编辑:
正如edg指出的那样,这并不妨碍 PowerShell 在执行 (5 + “5”) 时将“5”解释为整数。 我进行了更多挖掘,根据 Windows PowerShell 实际操作 中的 Bruce Payette 的说法,PowerShell实际上是一种“类型混杂的语言”。 所以,我想,我的答案是“有点”。
It can be if you need it to be.
Like so:
Use the [type] notation to indicate if you care about variables being strongly typed.
EDIT:
As edg pointed out, this doesn't prevent PowerShell from interpreting "5" as an integer, when executing (5 + "5"). I dug a little more, and according to Bruce Payette in Windows PowerShell in Action, PowerShell is actually a "type-promiscuous language." So, I guess, my answer is "sort of."
从技术上讲,它是一种强类型语言。
您可以拒绝在 shell 中声明类型,从而使其表现得像动态类型脚本语言,但它会将弱类型对象包装在“PsObject”类型的包装器中。 通过使用“New-Object”语法声明对象,对象是强类型的并且不是包装的。
Technically it is a strongly typed language.
You can decline to declare types in the shell, allowing it to behave like a dynamic typed scripting language, but it will wrap weakly-typed objects in a wrapper of type "PsObject". By declaring objects using the "New-Object" syntax, objects are strongly typed and not wrappered.
我认为您需要定义“强类型”的含义:
--维基百科
I think you will need to define what you mean by "Strongly Typed":
--Wikipedia
我撤回我之前的回答——引用如下。 我应该说一些更微妙的话,例如:
PowerShell 具有强大的类型系统,具有强大的类型推断功能,并且是动态类型的。
在我看来,这里存在几个问题,因此需要一个答案对“强类型语言”的含义进行更好的定义可能在他们解决问题的方法上更加明智。
由于 PowerShell 跨越了许多边界,因此 PowerShell 所在位置的答案可能存在于由以下区域组成的维恩图中:
I retract my previous answer -- quoted below. I should have said something more nuanced like:
PowerShell has a strong type system with robust type inference and is dynamically typed.
It seems to me that there are several issues at work here, so the answers asking for a better definition of what was meant by a "strongly-typed language" were probably more wise in their approach to the question.
Since PowerShell crosses many boundaries, the answer to where PowerShell lies probably exists in a Venn diagram consisting of the following areas: