PowerShell 是强类型语言吗?

发布于 2024-07-05 16:14:46 字数 41 浏览 6 评论 0原文

PowerShell 肯定属于动态语言的范畴,但它会被视为强类型吗?

PowerShell is definitely in the category of dynamic languages, but would it be considered strongly typed?

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

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

发布评论

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

评论(7

下雨或天晴 2024-07-12 16:14:46

这些术语存在一定程度的混乱。 本文解释了一种有用的类型分类法系统。

PowerShell 是动态、隐式类型的:

> $x=100
> $x=dir

没有类型错误 - 变量可以在运行时更改其类型。 这就像 PythonPerlJavaScript 但是与 C++Java, C# 等。

但是:

> [int]$x = 100
> $x = dir
Cannot convert "scripts-2.5" to "System.Int32".

因此,如果您愿意,它还支持显式变量输入。 但是,类型检查是在运行时而不是编译时完成的,因此它不是静态类型的。

我看到有人说 PowerShell 使用类型推断(因为您不必声明变量的类型),但我认为这是错误的说法。 类型推断是在编译时进行类型检查的系统功能(如 C# 中的“var”)。 PowerShell 仅在运行时检查类型,因此它可以检查实际值而不是进行推理。

但是,正在进行一定量的自动类型转换:

> [int]$a = 1
> [string]$b = $a
> $b
1
> $b.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

因此某些类型会即时转换。 根据大多数定义,这将使 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:

> $x=100
> $x=dir

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:

> [int]$x = 100
> $x = dir
Cannot convert "scripts-2.5" to "System.Int32".

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:

> [int]$a = 1
> [string]$b = $a
> $b
1
> $b.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

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.

荒岛晴空 2024-07-12 16:14:46

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).

尤怨 2024-07-12 16:14:46

我认为进一步查看向 Int 添加 String 示例将为讨论提供更多依据。 什么被认为是动态类型转换? 其中一条评论中有人说,在这种情况下:

4 + "4"

“4”变成了 Int32。 我根本不相信情况是这样。 我认为,中间步骤发生在命令更改为:

4 + [System.Convert]::ToInt32("4")

请注意,这意味着 "4" 在整个过程中保持字符串状态。 为了证明这一点,请考虑以下示例:

19# $foo = "4"
20# $foo.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


21# 4 + $foo
8
22# $foo.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

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:

4 + "4"

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:

4 + [System.Convert]::ToInt32("4")

Note that this means that "4" stays a String through the entire process. To demonstrate this, consider this example:

19# $foo = "4"
20# $foo.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


21# 4 + $foo
8
22# $foo.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
美人迟暮 2024-07-12 16:14:46

如果您需要的话,它可以是。

像这样:

[1] » [int]$x = 5
[2] » $x
5
[3] » $x = 'haha'
Cannot convert value "haha" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:3
+ $x  <<<< = 'haha'
[4] »

使用 [type] 表示法来指示您是否关心变量是强类型的。

编辑

正如edg指出的那样,这并不妨碍 PowerShell 在执行 (5 + “5”) 时将“5”解释为整数。 我进行了更多挖掘,根据 Windows PowerShell 实际操作 中的 Bruce Payette 的说法,PowerShell实际上是一种“类型混杂的语言”。 所以,我想,我的答案是“有点”。

It can be if you need it to be.

Like so:

[1] » [int]$x = 5
[2] » $x
5
[3] » $x = 'haha'
Cannot convert value "haha" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:3
+ $x  <<<< = 'haha'
[4] »

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."

千笙结 2024-07-12 16:14:46

从技术上讲,它是一种强类型语言。

您可以拒绝在 shell 中声明类型,从而使其表现得像动态类型脚本语言,但它会将弱类型对象包装在“PsObject”类型的包装器中。 通过使用“New-Object”语法声明对象,对象是强类型的并且不是包装的。

$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters

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.

$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters
还如梦归 2024-07-12 16:14:46

我认为您需要定义“强类型”的含义:

在计算机科学和计算机编程中,术语“强类型”用于描述编程语言对涉及不同数据类型的值的操作如何混合指定一个或多个限制的情况。 反义词是弱类型。 然而,在计算的短暂历史中,这些术语被赋予了如此广泛的含义,以至于很难脱离上下文来了解个别作者在使用它们时的含义。

--维基百科

I think you will need to define what you mean by "Strongly Typed":

In computer science and computer programming, the term strong typing is used to describe those situations where programming languages specify one or more restrictions on how operations involving values having different datatypes can be intermixed. The antonym is weak typing. However, these terms have been given such a wide variety of meanings over the short history of computing that it is often difficult to know, out of context, what an individual writer means when using them.

--Wikipedia

飞烟轻若梦 2024-07-12 16:14:46

我撤回我之前的回答——引用如下。 我应该说一些更微妙的话,例如:

PowerShell 具有强大的类型系统,具有强大的类型推断功能,并且是动态类型的。

在我看来,这里存在几个问题,因此需要一个答案对“强类型语言”的含义进行更好的定义可能在他们解决问题的方法上更加明智。

由于 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:

  • Static vs. dynamic type checking
  • Strong vs. weak typing
  • Safe vs. unsafe typing
  • Explicit vs. implicit declaration and inference
  • Structural vs. nominative type systems

"PowerShell is a strongly typed language.

However, it only requires you to declare the type where there is ambiguity.

If it is able to infer a type, it does not require you to specify it."

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