πŸ“œ ⬆️ ⬇️

Likbez on typing in programming languages

image

This article contains the necessary minimum of those things that you just need to know about typing, so as not to call dynamic typing evil, Lisp - a typeless language, and C - a language with strong typing.

The full version contains a detailed description of all types of typing, seasoned with code examples, links to popular programming languages ​​and demonstrative pictures.

I recommend reading first a short version of the article, and then if you have the desire and complete.
')

Short version


Typing programming languages ​​are usually divided into two large camps - typed and untyped ( typeless ). The first example includes C, Python, Scala, PHP, and Lua, and the second is assembly language, Forth and Brainfuck.

Since β€œtypeless typing” is inherently as simple as a cork, it is not further divided into any other types. But typed languages ​​are divided into several overlapping categories:


It should also be noted that all these categories overlap, for example, the C language has a static weak explicit typification, and the Python language has a dynamic strong implicit one.

However, there are no languages ​​with static and dynamic typifications at the same time. Although looking ahead I will say that here I am lying - they really exist, but more on that later.

Let's go further.

Detailed version


If the short version was not enough for you, good. No wonder I wrote a detailed? The main thing is that in the short version it was simply impossible to fit all the useful and interesting information, and the detailed information might be too long for everyone to read it without straining.

Typeless typing


In typeless programming languages, all entities are considered simply sequences of bits, of various lengths.

Typeless typing is usually inherent in low-level (assembly language, Forth) and esoteric (Brainfuck, HQ9, Piet) languages. However, it, along with disadvantages, has some advantages.

Benefits


disadvantages


Strong typeless typing?

, . ( 86/86-64, ) , cx (16 ) rax (64 ).

mov cx, eax ;

, - ? , . , , .



image

, (static) (dynamic) , , .

, ( , ). , β€” , ? ? , , ?

.





image


, β€” . β€” ( ) β€” , .

? 3- : .

β€” . , ( ) , , β€” (-1).

(Python):
def find( required_element, list ):
    for (index, element) in enumerate(list):
        if element == required_element:
            return index

    return (-1)


, , , , . . β€” - !

():
unsigned int find_int( int required_element, int array[], unsigned int size ) {
    for (unsigned int i = 0; i < size; ++i )
        if (required_element == array[i])
            return i;

    return (-1);
}

unsigned int find_float( float required_element, float array[], unsigned int size ) {
    for (unsigned int i = 0; i < size; ++i )
        if (required_element == array[i])
            return i;

    return (-1);
}

unsigned int find_char( char required_element, char array[], unsigned int size ) {
    for (unsigned int i = 0; i < size; ++i )
        if (required_element == array[i])
            return i;

    return (-1);
}


, Python, ? ?

, . , . C++ . :

( , C++):
template <class T>
unsigned int find( T required_element, std::vector<T> array ) {
    for (unsigned int i = 0; i < array.size(); ++i )
        if (required_element == array[i])
            return i;

    return (-1);
}


! Python . , 3-, !

, β€” .

, , . - ( Haskell). - ( ), () .


, , :


, :


, ?



image

. Β« Β». β€” strong typing.

, , , . Β« Β». β€” weak typing.

, . .

, . , , . !

. , , , , , «» ? β€” , ! (, )

- ? , , , , .

?





, , ! ?

.

,

… . Β« Β» ?

, . β€” . β€” ( , , , -, , ).

. 3.5 β€” ( β€” ? ? ? ?).

β€” , . .

, PL/I . ! !

, . ? , Pascal , . C#, Groovy Common Lisp.

, , . , .

Haskell.

, .

pi + 1, pi + 1.0 pi + float(1). pi + 1!

Haskell, , 1 . , , . !

sum x y, x y ( 1), β€” sum , sum , sum , sum sum .

, .

, , . , ( Haskell, Java, C#, Python), ( C, JavaScript, Lua, PHP).

, ?



image

, , . β€” explicit typing.

, , . β€” implicit typing.

- , , β€” , , .

, , ?





, , ( - ?), !

-

, - . . β€” Haskell, , :
--    
add (x, y) = x + y

--   
add :: (Integer, Integer) -> Integer
add (x, y) = x + y


: , add :: (Num a) => a -> a -> a*, .. , Haskell'.

* int_index .

. , . 18 , !

, Haskell, . ( )

- -?
.

-

C++, C++11 ( C++0x), auto, , :
 :
//   
unsigned int a = 5;
unsigned int b = a + 3;

//   
unsigned int a = 5;
auto b = a + 3;


. . ( , , , ):
//   
std::vector<int> vec = randomVector( 30 );
for ( std::vector::const_iterator it = vec.cbegin(); ... ) { 
    ...
}

//   
auto vec = randomVector<int>( 30 );
for ( auto it = vec.cbegin(); ... ) { 
    ...
}


! . , - Haskell, ?

, decltype auto:
//   
int divide( int x, int y ) {
    ...
}

//   
auto divide( int x, int y ) -> decltype(x / y) {
    ...
}


, , (templates / generics) .



β€œβ€.

JavaScript  -  |       | 
Ruby        -  |      | 
Python      -  |      | 
Java        -   |      | 
PHP         -  |       | 
C           -   |       | 
C++         -   |       | 
Perl        -  |       | 
Objective-C -   |       | 
C#          -   |      | 
Haskell     -   |      | 
Common Lisp -  |      | 
D           -   |      | 
Delphi      -   |      | 


( C# qxfusion):


- , CL, PHP Obj-C, - β€” .


. , . ? ? ? , .

!


:
:

Source: https://habr.com/ru/post/161205/


All Articles