A Conversion With Other Than One Operand Is Refused

Status: refused by design

Number(1, 2) is not FunctionalScript. Number(x, y) is valid JavaScript, and fjs compile refuses it by design: the demo below shows what it prints.

Number(x) and String(x) are conversions, operators with one operand, not calls of a function value. JavaScript ignores a second argument and converts undefined when there is none, and a spread hides how many there are; FunctionalScript refuses each shape but the one operand, so a conversion always says what it converts.

The row op.conversion-call-shape of refused constructs registers the refusal, and operators and functions gives its reason.

Examples

// @ts-expect-error: TypeScript refuses the second operand too
export default Number("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: 1.

The extra argument in sample.mjs is intentional. A code-quality check correctly reports a superfluous argument to Number; removing it would turn the negative example into an accepted conversion and stop testing this refusal.

What to Write Instead

Convert one value, as alternative.f.js does:

export default Number("1");

It answers 1.

A Better Message

Today fjs compile prints Number takes one argument 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:16 - error: Number takes one argument
2 | export default Number("1", 2);
  |                ^

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

sample.mjs:2:16 - error: `Number` is a conversion: it takes exactly one operand
2 | export default Number("1", 2);
  |                ^^^^^^ JavaScript ignores the `2` here
  = help: convert one value:
          export default Number("1");
  = note: refused by design, see spec/conversions/operands/README.md

JavaScript source, sample.mjs:

// @ts-expect-error: TypeScript refuses the second operand too
export default Number("1", 2);

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 conversion with its one operand
 * ([`README.md`](./README.md)).
 *
 * @module
 */

export default Number("1");

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

export default Number("1");

What it answers, its default export:

1

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs