Quantcast

Strict (non-coercing) expressions

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Strict (non-coercing) expressions

T.J. Crowder-2
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



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Strict (non-coercing) expressions

Claude Pache

> Le 13 avr. 2017 à 08:39, T.J. Crowder <[hidden email]> a écrit :
>
> 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 think that, in general, “implicit coercion” is not the real issue, although it makes things worse in some cases. The real issue is “ambivalent meaning”.

For example, `+` is so problematic in JS because it has two contradictory meanings: addition and concatenation.

On the other hand, `<` is less problematic, because there is much rarely a situation where it is ambiguous (for a human).

Another example:

```js
var a = new Date
var b = new Date(a)
a <= b // true
a < b // false
a == b // probably not what you mean
```

In this case, “stricter” is not helpful in the “equality” test. What would have helped, is comparison operators with non-ambivalent meanings.  E.g., Perl has `==` for numerical comparison, `eq` for string comparison, and, since v6, `eqv` for structural comparison, `===` for identity comparison, `=:=` for...

—Claude

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Loading...