Unveiling the Hidden Gems of Programming Languages: 5 Unique Features
Written on
Chapter 1: The Art of Language Design
Gilad Bracha, a renowned language designer, once likened the craft of programming language development to that of cooking. Just as a chef expertly combines various ingredients to create a delightful dish, a language designer merges different programming concepts and paradigms.
Experienced programmers are well-acquainted with concepts like dynamic dispatch, inheritance (both single and multiple), structural typing (as seen in Go), nominal typing (like Java), parametric polymorphism (generics), and higher-order functions. Some may even be familiar with advanced ideas such as multi-dispatch in Groovy or multi-methods found in languages from the Lisp family.
This article delves into some lesser-known, yet intriguing features of programming languages. When I say "intriguing," I refer to practical functionalities that aren’t typically present in mainstream languages.
Section 1.1: Resumable Exceptions
In most programming languages, when an exception occurs, the program looks up the call stack to find the closest exception handler. Once found, the program resumes from that point. This process is generally one-directional; you can't return to the original code that triggered the exception. However, Smalltalk offers resumable and retryable exceptions, which enhance flexibility. You don't need to know Smalltalk's syntax; here's how it would be represented in Java:
try {
System.out.println("Result: " + ((5 / 0) + 42));
} catch (ArithmeticException e) {
e.resume(3); // Hypothetical resume() method
}
In this example, dividing by zero throws an exception. The handler sends a resume message to the exception object, allowing the program to return to the original expression and continue from that point. If we specify a parameter, the expression will behave as if it had returned that value.
For instance, if we use a default value to retry an expression that fails, it may look like this:
[ self tryToCreateDirectoryAndSaveFile ]
on: DirectoryDoesNotExist
do: [ :ex | ex resume: '/tmp' ].
This approach allows the program to use a default directory when it cannot create the desired one.
Section 1.2: Retry Exception
Similar to resumable exceptions, retry exceptions allow the code to re-execute the faulty expression. For example:
[ 5 / #(1 0) atRandom ]
on: ZeroDivide
do: [ :ex | ex retry ]
Here, the program randomly chooses between 1 and 0. If a division by zero occurs, it will rerun the expression until successful.
Chapter 2: Unique Concepts in Programming
In the first video, "The Secret to Mastering Any Programming Language," the discussion revolves around effective strategies to learn and master programming languages.
The second video, "Programming Languages That Everyone Should Learn," features insights from George Hotz and Lex Fridman on essential programming languages for aspiring developers.
Subsection 2.1: Predicate Classes
During a job interview, I was asked about the definition of a "class." I opted for an unconventional answer, stating that a "class is a meta-object." While this is accurate, it didn’t align with the interviewer's expectations of a standard response.
In most programming languages, an object's class is immutable after creation. However, imagine a scenario where an integer's class dynamically changes based on its value—odd or even. If we were to use a FileInputStream, closing it could automatically change its class to ClosedFileInputStream.
Factor is one language that employs this unique mechanism.
Subsection 2.2: The Become Message
Another fascinating aspect of Smalltalk is the become message, which allows two objects to swap places throughout the program. For example:
Color white become: Color black
Executing this command alters all instances of white to black and vice versa. However, be cautious with potentially harmful operations like true become: false.
This feature can be particularly useful in testing scenarios, enabling the replacement of real implementations with mock objects.
Subsection 2.3: Accessing the Return Stack
Forth is another remarkable language that provides direct access to its return stack, facilitating the implementation of coroutines. Here’s a simple implementation:
: coroutine r> r> swap >r >r ;
This basic structure allows for the creation of coroutines, enabling dialogue-like interactions between functions, which can enhance control flow in certain applications.
In conclusion, these unique features illustrate the versatility and creativity that can be found in programming languages. Do you have any lesser-known language features that you appreciate? Feel free to share your thoughts!