Skip to content

Latest commit

 

History

History
217 lines (136 loc) · 7.39 KB

File metadata and controls

217 lines (136 loc) · 7.39 KB

Colloquial Classification of Programming Languages

There is no precise technical definition of what the terms mean and different authors disagree about the implied meaning of the terms and the relative rankings of the "strength" of the type systems of mainstream programming languages (Strong and weak typing - Wikipedia, 2022).

These definitions were not defined by me. I have grouped information from multiple sites, books and different authors to create definitions and examples that could fit with the "generally" accepted definitions according to my online research.

Classifications

Strongly vs Weakly Typed

Strongly typed

Strongly-typed languages do not allow implicit conversions between unrelated types. Strong typing, you will only be allowed operations on the data by direct manipulation of the objects of that data type. Variables and other data structures can be declared to be of a specific type and check the validity of their values.

Weakly Typed - Loosely Typed - Untyped Language

Weakly-typed languages make conversions between unrelated types implicitly. Weak typing allows you to operate on data without considering its type. You do not have to specify what type of information will be stored in a variable in advance.

Static vs Dynamic Typing

Dynamic Typing

Dynamically-typed languages perform type checking at runtime.

Static Typing

Statically typed languages perform type checking at compile time.

Examples

Go

  • Strongly Typed: Allows at any point to know exactly what type of value each variable has and allows operations according to their type.

Example 1

package main

func main() {
    var variableName string = "Juan"
    var variableNumber int = 97
    variableName = variableNumber
}

The above example will fail to compile as variableName is type string and variableNumber is type int with the error message cannot use 97 (type untyped int) as type string in assignment

  • Statically Typing: Go is statically typed because type is expected when declaring new variables.

Example 2

This example will fail to compile:

package main

func main() {
    var variableName
}

The error when running the go compiler will be syntax error: unexpected newline, expecting type as the variable variableName is missing the type declaration or initial value to identify the type.

Python

  • Strongly typed: Variable type str does not allow operations with type int or any other type other than str.

This will fail as the variable are not compatible:

variable_name: str = "1"
variable_number: int = 97
result_error = variable_name + variable_number

This will fail as the result would be Line 3 - TypeError: can only concatenate str (not "int") to str.

  • Dynamic typing: A variable type can be updated during runtime.

This will finish successfully as the variable_number initially as type number has been assigned a value of type string during runtime.

Example 1

variable_name: str = "Juan"
variable_number: int = 97
variable_number = "Carlos"
result_success = variable_name + " " + variable_number

The result_success will have a value of Juan Carlos.

Example 2

variable_number: int = 97
variable_number = "Juan"

The variable variable_number initially declared as integer can be modified during runtime to hold a value of type string as Juan.

PHP

  • Weakly typed: Variable type string allow operations with type integer.

Example 1

This will pass as the variable $variable_string_number is a string integer.

$variable_string_number = "1"
$variable_number = 97
$result_success = $variable_string_number + $variable_number

The result of the above operation would be 98 of type integer, as $variable_string_number is a numeric string.

Example 12

This will fail as the variable $variable_string is a string and does not allow arithmetic operation between non numeric strings and integer types.

$variable_string_number = "Juan"
$variable_number = 97
$result_fail = $variable_string_number + $variable_number

The result of the above operation would be Uncaught TypeError: Unsupported operand types: string + int [...]:3.

JavaScript

  • Weakly typed: Variable type string allow operations with type number.

This will be executed successfully as JavaScript allows implicit conversion between unrelated types during runtime.

let variableName = "Juan"
let variableNumber = 97
let resultSuccess = variableName + variableNumber 

The value that resultSuccess that will have at the end is Juan97.

  • Dynamic typing: A variable type can be updated during runtime.

This will finish successfully as the variableName can change its type dynamically.

let variableName;
variableName = "Juan";
variableNumber = 97;

The value that variableNumber will have at the end of the execution will be 97 and type of number.

TypeScript

  • Strongly typed: This will fail as Typescript does type check during compile.
let variableName = "Juan";
variableName = 5; //Type 'number' is not assignable to type 'string'.
  • Statically typing: TypeScript through type annotations enables type checking at compile time to avoid error.

This will fail as the variableName cannot be updated with a value that is not string type.

let variableName: string;
variableName = "Juan";
variableName = 97; //Type 'number' is not assignable to type 'string'.

Important note

As TypeScript is a superset of JavaScript that can ignore rules set by TypeScript to use dynamically typing using any that will be the equivalent to untyped variable on JavaScript.

Example 1

By not adding a variable type, by default the variableName has type any. The following example will pass successfully:

let variableName;
variableName = "Juan";
variableName = 97;

After compilation, and executing the variable variableName will have the value 97 and will be type of number.

Example 2

By adding a variable type as string and adding @ts-ignore. The following example will pass and compile to JavaScript successfully:

let variableName: string;
variableName = "Juan";
//@ts-ignore
variableName = 97;

References