== Is Refused

Status: refused by design

== is not FunctionalScript. a == b is valid JavaScript, and fjs compile refuses it by design: the demo below shows what it prints.

JavaScript's == and != convert their operands before comparing them, by rules that depend on both types: 0 == "" and 0 == "0" are true while "" == "0" is false, and null == undefined is true while null == 0 is false. A reader cannot tell what a comparison does without knowing both types, so the operators are refused rather than given a meaning of their own. === and !== compare without converting, and a conversion that is meant is written, Number(x) or String(x).

The row op.loose-equality of refused constructs registers the refusal, and operators gives its reason.

Examples

/** @type {(a: unknown, b: unknown) => boolean} */
const same = (a, b) => a == b;
export default same(0, "");

The demo on this page compiles sample.mjs, the module above, with this build's fjs compile, and shows the line it prints and the token it refuses. Below that it shows what JavaScript answers for the same module: true.

What to Write Instead

Compare with ===, and write the conversion you mean, Number(b) or String(a), as alternative.f.js does:

/** @type {(a: number, b: string) => boolean} */
const same = (a, b) => a === Number(b);
export default same(0, "");

It answers true. "" converts to 0, the conversion == made, now written where a reader sees it.

A Better Message

Today fjs compile prints unexpected token at the refused token. The demo proposes what it should print instead: the reason at the same token, what JavaScript does there, and the allowed spelling, the line alternative.f.js writes, as help. The page compiles the alternative too, so the suggestion is one the compiler accepts.

Reference

Demo

Not allowed in FunctionalScript

This is valid JavaScript, and fjs compile refuses it, by design: it is not a FunctionalScript module.

What fjs compile sample.mjs output.js prints:

sample.mjs:2:26 - error: unexpected token
2 | const same = (a, b) => a == b;
  |                          ^

What it should print instead, pointing to what FunctionalScript allows (proposed):

sample.mjs:2:26 - error: `==` is not FunctionalScript: it converts its operands by rules that depend on both types
2 | const same = (a, b) => a == b;
  |                          ^^ JavaScript converts `""` to `0` here and answers `true`
  = help: compare with `===`, and write the conversion you mean, `Number(b)` or `String(a)`:
          const same = (a, b) => a === Number(b);
  = note: refused by design, see spec/operators/loose-equality/README.md

JavaScript source, sample.mjs:

/** @type {(a: unknown, b: unknown) => boolean} */
const same = (a, b) => a == b;
export default same(0, "");

What JavaScript answers for the same module, its default export:

true

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): the conversion `==` makes in secret, written, and the comparison `===`, which converts nothing
 * ([`README.md`](./README.md)).
 *
 * @module
 */

/** @type {(a: number, b: string) => boolean} */
const same = (a, b) => a === Number(b);
export default same(0, "");

What fjs compile alternative.f.js output.js writes:

const $0=($1,$2)=>$1===Number($2);export default $0(0,"");

What it answers, its default export:

true

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs