views:

975

answers:

8

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

A: 

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.

Factor Mystic
Wikipedia is not infallable...
Guy Starbuck
This doesn't answer the question.
Ed Guiness
Edg is right, this does not answer the question. Basically, dynamic typing is an opposite to static typing, not strong typing. You can have a dynamically typed strongly typed language.
Paul Batum
+2  A: 

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

David Mohundro
Nope. You can add a string to an integer and get a numeric result. If you reverse the order of addition you get concatenation.
Ed Guiness
I think this shows that it *can* be statically typed which does not mean that it *must* be strongly typed.
EBGreen
So, it's weakly and statically typed?
John Millikin
Sort of both, thus the "promiscuous" part. :)
David Mohundro
A: 

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
Guy Starbuck
A: 

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

Timothy Lee Russell
A: 

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

EBGreen
+1  A: 

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.

JacquesB
+1  A: 

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
EBGreen
Good point - thanks!
David Mohundro
You can however change the type of a variable through assignment. If you do $foo="4" and then $foo = 8 + $foo, then the type of $foo will change from string to int. Values are immutable, though.
JacquesB
A: 

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

The Old Hag