Many languages have assert(). This post describes how assert() is implemented in Next Generation Shell and how these aspects/features are consistent w

Next Generation Shell

submited by
Style Pass
2024-10-16 08:30:01

Many languages have assert(). This post describes how assert() is implemented in Next Generation Shell and how these aspects/features are consistent with the rest of the language.

Following the observation that in many cases predicate callbacks just compare the given value to a pattern, most if not all methods in NGS that were previously taking a predicate argument were upgraded to take a pattern argument instead. Following that development, assert(EXPR) got the assert(EXPR, PATTERN) flavor, which turned out to be used way more frequently than the “regular” assert(EXPR).

Usage of patterns in assert() is completely aligned with the rest of the language, where many other methods support pattern arguments.

assert(EXPR, PATTERN), can of course throw an exception. When it doesn’t, it evaluates to EXPR, implementing fluent interface. This leads to the following idiomatic usages:

In object oriented languages, in order to chain assert()s, you would have to have the .assert() as a method in each class (could be inherited maybe). That would be weird to have such “pollution”. In NGS, since there are no classes, the assert() method is defined outside of any class, like all other methods, and works with any types of EXPR and PATTERN that implement pattern matching (EXPR =~ PATTERN).

Leave a Comment