PowerShell is definitely in the category of dynamic languages, but would it be considered strongly typed?
From Wikipedia:
Windows PowerShell includes a dynamically typed scripting language which can implement complex operations using cmdlets imperatively.
So there you go.
E: This is wrong. Luckily, none of us is as dumb as all of us.
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 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."
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.
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 its not statically typed.
I have seen some say that PowerShell uses type inference (because you dont 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-convesion 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.
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
Powerhshell is dynamically typed plain and simple. It is described as such by its createor 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 dynamic during cast. Any language that allows you to assign a string to a var and print it out and then assign a number to the same var and do calculations with it is dynamically typed.
Additionally, powershell is dynamically scoped (if anyone here knows what that means).