[ACCEPTED]-Is PowerShell a strongly-typed language?-strong-typing

Accepted answer
Score: 26

There is a certain amount of confusion around 24 the terminlogy. This article explains a useful taxonomy 23 of type systems.

PowerShell is dynamically, implicit 22 typed:

> $x=100
> $x=dir

No type errors - a variable can change 21 its type at runtime. This is like Python, Perl, JavaScript but 20 different from C++, Java, C#, etc.

However:

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

So it also 19 supports explicit typing of variables if you want. However, the 18 type checking is done at runtime rather 17 than compile time, so it's not statically typed.

I 16 have seen some say that PowerShell uses 15 type inference (because you don't have to declare the 14 type of a variable), but I think that is 13 the wrong words. Type inference is a feature 12 of systems that does type-checking at compile 11 time (like "var" in C#). PowerShell 10 only checks types at runtime, so it can 9 check the actual value rather than do inference.

However, there 8 is some amount of automatic type-conversion 7 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 6 fly. This will by most definitions make 5 PowerShell a weakly typed language. It is certainly 4 more weak than e.g. Python which (almost?) never 3 convert types on the fly. But probably not 2 at weak as Perl which will convert almost 1 anything as needed.

Score: 6

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 9 the [type] notation to indicate if you care 8 about variables being strongly typed.

EDIT:

As 7 edg pointed out, this doesn't prevent PowerShell 6 from interpreting "5" as an integer, when 5 executing (5 + "5"). I dug a little 4 more, and according to Bruce Payette in 3 Windows PowerShell in Action, PowerShell is actually a "type-promiscuous 2 language." So, I guess, my answer is 1 "sort of."

Score: 1

Technically it is a strongly typed language.

You 6 can decline to declare types in the shell, allowing 5 it to behave like a dynamic typed scripting 4 language, but it will wrap weakly-typed 3 objects in a wrapper of type "PsObject". By 2 declaring objects using the "New-Object" syntax, objects 1 are strongly typed and not wrappered.

$compilerParameters = New-Object System.CodeDom.Compiler.CompilerParameters
Score: 1

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

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

--Wikipedia

Score: 1

I think looking at the adding a String to 10 an Int example further would provide more 9 grist for the discussion mill. What is considered 8 to be dynamic type casting? Someone in one 7 of the comments said that in this case:

4 + "4"

The 6 "4" becomes an Int32. I don't believe that is the case at all. I 5 believe instead that an intermediate step 4 happens where the command is changed to:

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

Note 3 that this means that "4" stays a String through 2 the entire process. To demonstrate this, consider 1 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
Score: 1

PowerShell is dynamically typed, plain and 13 simple. It is described as such by its creator, Bruce 12 Payette.

Additionally, if anyone has taken 11 a basic programming language theory class 10 they would know this. Just because there 9 is a type annotation system doesn't mean 8 it is strongly typed. Even the type annotated 7 variables behave dynamically during a cast. Any 6 language that allows you to assign a string 5 to a variable and print it out and then 4 assign a number to the same variable and 3 do calculations with it is dynamically typed.

Additionally, PowerShell 2 is dynamically scoped (if anyone here knows 1 what that means).

More Related questions