foolang
foolang copied to clipboard
Range check expressions
Expressions to determine if a value is in a range are relatively commonplace.
A compound expression along the lines of 1 <= x <= 10
is marginally easier to read, in comparison to using logical operators to combine expressions: 1 <= x and: x <= 10
- but more importantly the compound expression has less things to go wrong when writing as x
is referenced only once.
Below some possibilities, but see also Fortress and Lunar for other implementation strategies.
Option 1: keyword messages
Use 1 <=: x <=: 10
instead.
Pro:
- Doesn't need new syntax or semantics, language core remains simple
- User-extensible
Con:
- Every combination needs to be defined separately. For
<,>,<=,>=
and three operands that's 16 different combinations, albeit maybe some combinations likex >: y <: z
should not be defined as they're more likely errors than intentional. - Looks a bit funny
Current favorite.
Option 2: let the parser deal with it silently
Turn this silently into let left = 1. let middle = x. let right = 10. right <= middle and middle <= left
.
Pro:
- Works out of the box for all combinations of comparisons
- Familiar & intuitive
Con:
- Complicates parsing, complicates the mental model user is required to have
- Not user-extensible until syntax is extensible in general
Current 2nd option.
Option 3: is-prefix for method syntax
Interpret is <receiver> [<binary-selector> <arg>]+
as a send of: #<binary>:<binary>:
.
Method definitions look like:
Minor extension of option 1, doesn't seem like an improvement, but a useless complication.
Option 4: is-prefix for parser
Require is-prefix: is 1 <= x <= 10
, which tells parser to use logical associativity in the binary message chain.
Pro:
- Works out of the box for all combinations of comparisons
- User-extensible
Con:
- Parsing changing depending on the prefix is pretty nasty
See also: https://srfi.schemers.org/srfi-156/srfi-156.html
No.
Option 5: right-handed generalized booleans
-
Number#<=
evaluates to a generalized boolean (False
or the right side), and similarly for other comparisons. -
Boolean#<=
evaluates toFalse
.
Pro:
- Works out of the box for all combinations of comparisons
Con:
- Generalized boolean as result of comparison, cannot store result of
x > 0
in a boolean variable, etc, OR need automatic coercion, which is terrible.
No.