var Is Refused

Status: refused by design

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

A var is a variable: it can be assigned again, it is visible before its declaration as undefined, and its scope is the whole function rather than its block. FunctionalScript binds a name with const, or with let as a variable of its block, declared before it is read (variables), so var is refused.

The row st.var of refused constructs registers the refusal, and constants gives its reason.

Examples

var a = 1;
export default a;

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.

What to Write Instead

Bind the name with const, as alternative.f.js does:

const a = 1;
export default a;

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:1:1 - error: unexpected token
1 | var a = 1;
  | ^

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

sample.mjs:1:1 - error: `var` is not FunctionalScript: a name is bound once, with `const`
1 | var a = 1;
  | ^^^ JavaScript declares a variable here, hoisted to the top of its function
  = help: bind the name with `const`:
          const a = 1;
  = note: refused by design, see spec/bindings/var/README.md

JavaScript source, sample.mjs:

var a = 1;
export default a;

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 binding, made once with `const`
 * ([`README.md`](./README.md)).
 *
 * @module
 */

const a = 1;
export default a;

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

export default 1;

What it answers, its default export:

1

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs