A Statement After return Is Refused

Status: refused by design

return 1; const a is not FunctionalScript. return 1; const a = 2; is valid JavaScript, and fjs compile refuses it by design: the demo below shows what it prints.

Nothing after return, throw, or a switch with a default ever runs. JavaScript admits the text and ignores it; FunctionalScript refuses it, since code that cannot run is a mistake or a leftover, and a reader should not have to work out that it is dead (functions).

This refusal applies after terminal statements in function bodies, guards and cases. Loop bodies retain statements after their explicit exits, including return and throw, so the same source checks apply after every loop transfer. Those statements perform no computations, but their declarations, references and writes still determine source legality.

The row st.unreachable of refused constructs registers the refusal, and functions gives its reason.

Examples

/** @type {() => number} */
const f = () => {
    return 1;
    const a = 2;
};
export default f();

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

The unreachable const a = 2; in sample.mjs is intentional. A code-quality check correctly reports it as unreachable; removing it would remove the construct this negative example must show the compiler refusing.

What to Write Instead

Remove what follows the return, as alternative.f.js does:

/** @type {() => number} */
const f = () => {
    return 1;
};
export default f();

It answers 1.

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:4:5 - error: unexpected token
4 |     const a = 2;
  |     ^

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

sample.mjs:4:5 - error: this statement never runs: nothing follows `return` in FunctionalScript
4 |     const a = 2;
  |     ^^^^^ JavaScript never reaches this
  = help: remove what follows the `return`:
              return 1;
  = note: refused by design, see spec/functions/unreachable/README.md

JavaScript source, sample.mjs:

/** @type {() => number} */
const f = () => {
    return 1;
    const a = 2;
};
export default f();

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

1

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): the same function, without the statement that never runs
 * ([`README.md`](./README.md)).
 *
 * @module
 */

/** @type {() => number} */
const f = () => {
    return 1;
};
export default f();

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

const $0=()=>1;export default $0();

What it answers, its default export:

1

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs