Strict (non-coercing) expressions
T.J. Crowder
tj.crowder at farsightsoftware.com
Thu Apr 13 06:39:45 UTC 2017
In the thread on [strict relational operators][1], felix [suggested][2] an
expression mode affecting all operators:
> Maybe every operator can have a non-coercing variant?
>
> One possible syntax is to have a modifier on operators
>
> x = a (<) b (+) c (&&) (!)d;
>
> if (x (!=) y) ...
>
>
> Another possible syntax is to have a modifier on expressions
>
> x = #(a < b + c && !d)
>
> if #(x != y) ...
I though the idea deserved its own thread. It's much broader in scope than
strict relational operators, and not exclusive with them.
I quite this idea of an expression mode making all operators within it
non-coercing, and having them throw a `TypeError` when presented with mixed
types. Using flagged parentheses (I didn't want to say "decorated") seems
like a great way to do it, too:
```js
if #(a > b) { /*...*/ }
while #(a > b) { /*...*/ }
for #(let i = 0; i < a.length && a > b; ++i) { /*...*/ }
if (answer == 42 && #(a > b)) { /*...*/ }
```
(Note the intentially-loose `answer == 42` in that last one.)
Which raises the question of possibly extending that concept to the block
level, where all expressions in the block are strict:
```js
#{
if (a > b) { /* ... */ }
// ^-- Throws if a and b are not the same type
}
```
If you like strict comparisons, a `#{ /*...*/ }` wrapper around all the
code in your module is all you need...
Or for just a function:
```js
function foo() #{ // <== Note the #
}
const foo = () => #{
}
```
I wonder if people would then ask for an explicitly-loose version of `!`
(or, indeed, other operators)... :-)
-- T.J. Crowder
[1]: https://esdiscuss.org/topic/strict-relational-operators
[2]: https://esdiscuss.org/topic/strict-relational-operators#content-5
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170413/968a8f99/attachment.html>
More information about the es-discuss
mailing list