A Global Is Refused

Status: refused by design

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

A FunctionalScript module has no free names: everything it reads it binds or imports. JavaScript's globals, Math, JSON, globalThis, Promise and the rest, are no exception, so a name no const, parameter or import binds is refused (const not found). The few built-ins the language admits, such as Number, String and Object.is, are recognized by name (built-ins).

The row name.unbound of refused constructs registers the refusal, and global names gives its reason.

Examples

export default Math.max(1, 2);

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

What to Write Instead

Write the function, or import it from a module, as alternative.f.js does:

/** @type {(a: number, b: number) => number} */
const max = (a, b) => a < b ? b : a;
export default max(1, 2);

It answers 2.

A Better Message

Today fjs compile prints const not found 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:16 - error: const not found
1 | export default Math.max(1, 2);
  |                ^

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

sample.mjs:1:16 - error: `Math` is not bound: a FunctionalScript module reads only what it binds or imports
1 | export default Math.max(1, 2);
  |                ^^^^ JavaScript reads the global `Math` here
  = help: write the function, or import it from a module:
          const max = (a, b) => a < b ? b : a;
  = note: refused by design, see spec/bindings/globals/README.md

JavaScript source, sample.mjs:

export default Math.max(1, 2);

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

2

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): the function the global would supply, written in the module
 * ([`README.md`](./README.md)).
 *
 * @module
 */

/** @type {(a: number, b: number) => number} */
const max = (a, b) => a < b ? b : a;
export default max(1, 2);

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

const $0=($1,$2)=>$1<$2?$2:$1;export default $0(1,2);

What it answers, its default export:

2

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs