Absolute bars - Manhunter07/MFL GitHub Wiki
As one of only a very few languages, MFL supports the use of many mathematical structures, including so-called absolute bars. They are used for obtaining the absolute value of a value they surround. Absolute bars act like parentheses, but are incapable of being used to define function arguments. Absolute values are any terms inside expressions that are located between two vertical line characters (|
). The result data-type of an absolute expression results from the value data-type of the input expression and is always identical. For structured value types the absolute operation is performed uppon each member and a new entity is returned.
Absolute bars can be nested. Because the opening and the closing sequence looks identical, the compiler will automatically detect if a specific bar character represents an opening or a closing tag. For readabilitty reasons it is however recommended to nest such sub-terms in seperate parentheses. Like parentheses, brackets or braces, absolute bars must be closed once they are opened. Leaving an unterminated (unclosed) absolute expression will result in a compiler error.
Operations
Data-type | Result |
---|---|
Boolean | True |
Number | Absolute (unsigned, positive) value |
String | Case-insensitive (uppercase) value |
Array | Array with absolute elements |
Record | Record with identical field names and absolute field values |
Null values and references do not support being used in absolute bars. Their use as a result of a term (or member of a term, if it is an array or a record) encapsulated in absolute bars will result in an exception.
Examples
The use of absolute bars around a negative number returns its corresponding positive value:
|-3| \returns 3\
The value of a positive number does not change when used between absolute bars:
|4.5| \returns 4.5\
A similar behaviour appears when absolute bars are used with string values. Here, they ensure the resulting string will only consist of uppercase letters and special characters:
|"Hello"| \returns "HELLO"\
|"WORLD"| \returns "WORLD"\
Arrays and records keep their structure, but change their member values by invoking the absolute operation uppon them. Their members will change if they are (or contain) negative numbers or characters. Otherwise, they will remain unchanged.
|[-1.2, 5] + ["Foo", "BAR"]]](/Manhunter07/MFL/wiki/\returns-[1.2,-5,-["FOO",-"BAR")\
|{Name = "MyEntity", ID = {Context = "MAIN", Index = 0}}| \returns {Name = "MYENTITY", ID = {Context = "MAIN", Index = 0}}\
Absolute bars may be used as sub-terms inside absolute bars as well:
|2 - |-4|| \returns 2\
Absolute bars as function reference
Like all operators, language constructs like the absolute bars can be passed as a function reference if needed. This can for example be useful when this operation has to be passed as a function argument. For this use case there exists a pre-declared reference constant named Abs
. This is not a function itself as the name might suggest, but a constant storing an anonymous function reference that performs an absolute operation.