PowerShell is definitely in the category of dynamic languages, but would it be considered strongly typed?
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.
$obj.SetFlag("None")
in the console with an implicit convert is way easier than $obj.SetFlag([Company.Example.Product.Data.Transfer.Enums.Flags]::None)
–
Tetragram 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."
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
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.
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
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).
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."
© 2022 - 2024 — McMap. All rights reserved.