instanceof Other Than Array Is Refused

Status: refused by design

instanceof is not FunctionalScript. [] instanceof Map is valid JavaScript, and fjs compile refuses it by design: the demo below shows what it prints.

x instanceof Array is in the language (instanceof): an array is the one kind of value with a class of its own. Any other right operand is refused, since JavaScript answers it by walking the prototype chain of x for that class's prototype, and FunctionalScript has neither the chain nor other classes to name.

The row op.instanceof of refused constructs registers the refusal, and instanceof gives its reason.

Examples

export default [] instanceof Map;

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: false.

What to Write Instead

Ask whether a value is an array with instanceof Array, as alternative.f.js does:

export default [] instanceof Array;

It answers true.

A Better Message

Today fjs compile prints the right operand of instanceof must be Array 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:1:19 - error: the right operand of instanceof must be Array
1 | export default [] instanceof Map;
  |                   ^

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

sample.mjs:1:19 - error: `instanceof` takes only `Array`: FunctionalScript has no other classes
1 | export default [] instanceof Map;
  |                   ^^^^^^^^^^ JavaScript walks the prototype chain of `[]` for `Map.prototype` here
  = help: ask whether a value is an array with `instanceof Array`:
          export default [] instanceof Array;
  = note: refused by design, see spec/operators/instanceof/other-classes/README.md

JavaScript source, sample.mjs:

export default [] instanceof Map;

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

false

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): the one instance check the language has, `instanceof Array`
 * ([`README.md`](./README.md)).
 *
 * @module
 */

export default [] instanceof Array;

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

export default [] instanceof Array;

What it answers, its default export:

true

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs