Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PowerShell a strongly-typed language?

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

like image 331
Chris Sutton Avatar asked Sep 22 '08 15:09

Chris Sutton


People also ask

Which languages are strongly typed?

Examples of strongly typed languages in existence include Java, Ruby, Smalltalk and Python. In the case of Java, typing errors are detected during compilation Other programming languages, like Ruby, detect typing errors during the runtime.

What datatype is PowerShell?

PowerShell data types include integers, floating point values, strings, Booleans, and datetime values. Variables may be converted from one type to another by explicit conversion, such as [int32]$value . In Windows PowerShell, single quotes display literal strings as is, without interpretation.

Is SQL a strongly typed language?

SQL is a strongly typed language. That is, every data item has an associated data type which determines its behavior and allowed usage.

Is Python a strongly typed?

Python is both a strongly typed and a dynamically typed language. Strong typing means that variables do have a type and that the type matters when performing operations on a variable. Dynamic typing means that the type of the variable is determined only during runtime.


1 Answers

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.

like image 139
JacquesB Avatar answered Sep 29 '22 18:09

JacquesB