Data Types
In addition to basic data
types for variables such as int
, float
, string
. Pike
supports arrays, sets, hash tables, and also a special data type called tagged
unions. Various operations, such as finding all unique elements in an array or
adding and removing elements to a set or array, can be easily done within few lines
of code. (This is similar to Python.)
Tagged unions enable
programmers to define variables that can contain values of different data
types. For instance, it's possible to declare a variable in Pike that can
accept both float
and int
values:
int | float number; number = 314; number = 3.14;
Or a variable that accepts
both int
and string
values:
int | string number; number = 256; number = "this is a test";
Furthermore, a variable can
have a special type called mixed
, meaning that it can contain data of any possible
type. This is an ideal feature to implement polymorphic functions. It is up to
the programmer to apply proper casting when a mixed
variable is assigned to
another variable of a certain type. For instance, if the programmer knows that
a mixed
type variable has an integer value at some point in the code, it is
possible to cast it to integer by putting the target type in the brackets:
mixed m; int n = [int]m;
In this way, Pike knows that
the mixed
variable is actually an integer variable and nothing
more. Using mixed
variables, it's possible to write functions that
check the type of the input variables at runtime and take different actions
according to the detected data type. Moreover, functions with a variable number
of input parameters can be implemented efficiently in Pike using tagged unions:
int fun (int x, int y, void | int z);
Pike is a strongly typed language with static type-checking. This makes
the language quite strict when it comes to casting between different data
types. Nevertheless, type promotion is a part of the language where implicit
conversion is applied and two variables of int
and float
are involved in a
mathematical operation, and the compiler automatically casts the int
variable to float
when needed:
> int x=10; > float y=1.1; > x+y; Result: 11.1
Ada programmers and software developers who have worked with safety-critical systems will recognize that these features help ensure robustness.
Memory Management
The memory management is (by default) done using a garbage-collection mechanism. However, it is possible to force the garbage collector to reclaim memory allocated to objects that are no longer used by the program. The GC in Pike uses reference counting to identify the objects that are candidates to be marked as garbage. The GC algorithm is further improved by utilizing a cycle-detection algorithm to find cyclic references that can be missed by standard GC.
Text Processing
Pike is a powerful language
for text processing. Its many routines that perform string operations have made
Pike a worthy competitor to languages such as Perl. It is also possible to
manipulate a string, as in Python, by using overloaded mathematical operators.
For instance, delimiters and substring joining are implemented in Pike using "/
"
and "*
" operations, respectively, and "+
" and "-
" can be used
to concatenate or remove a substring. For example:
> ("this is a test" / " ") * ";"; (1) Result: "this;is;a;test"
String formatting and
analysis is identical to C, where sprintf
and sscanf
are utilized. Furthermore, wide strings as well as
regular expressions are supported. All these features have made Pike a good tool
for text-processing applications such as Web servers and parsers.
Concurrency
Pike has a rich library for concurrent applications and for simply launching multiple threads simultaneously, message passing, and mutual execution. The concurrency model in Pike is synchronous message passing. A communication channel, which can be a queue or FIFO, is used for sending and receiving messages between concurrent threads.
Moreover, threads can read/write from/to the shared channels if the channel is not empty/full. If the channel is full or empty, and a thread intends to write or read from it, the thread will go to sleep mode until the data or the channel is available. It is also possible to send signals to a sleeping thread to wake it up at any time.
Conclusion
Pike is a multipurpose programming language that can be useful for a large class of applications and problems. Features such as tagged unions bring flexibility to the code and let the programmer implement complex structures in an elegant, simple, and readable way, which will ease further development, debugging, and maintenance in software projects.
The powerful functionalities and diverse functions and libraries that Pike offers for text processing, together with the flexible structure of the language, let developers minimize coding time, while ensuring robustness and efficiency in their code. In addition, existing modules for cryptography, network protocols, image processing, file handling, and big numbers make many tasks straightforward.
Despite all this goodness, a few important items remain on the Pike team's to-do list. These include true native compilation and better tools (especially an IDE), and good debugging support. But even without these desirable aspects, I think you'll find Pike to be a fun and useful language to work with.
Danesh Daroui is a software developer working at Opera Software's office in Gothenburg, Sweden. He is a member of the Turbo team developing solutions that aim to accelerate browsing speed in Opera's Web browsers.