Skip to content

CS2 Discussion: Question: Is conflating null and undefined a feature of CoffeeScript? #4945

@coffeescriptbot

Description

@coffeescriptbot

From @GeoffreyBooth on 2016-10-19 07:15

In CoffeeScript 1.x, null and undefined are often interchangeable:

  • if foo? returns false whether foo is null or whether foo is undefined.
  • ((a = 1) -> a)(null) and ((a = 1) -> a)(undefined) both return 1.

In ES2015, at least regarding default function parameters, null and undefined are not interchangeable:

  • (function (a = 1) { return a; })(null) returns null.
  • (function (a = 1) { return a; })(undefined) returns 1.

This has sparked a discussion in the PR where I’m trying to implement arrow functions (and by extension, ES-style default parameters) for CoffeeScript 2. Basically, if we want to preserve the current behavior where the default value is applied when a parameter is null or undefined, we need to keep the current generated output:

f = function(a) {
  if (a == null) {
    a = 1;
  }
  return a;
}

So if we want output like this:

f = function(a = 1) {
  return a;
};

Then we have to break backward compatibility and go with ES’ implementation of default parameter values applying only when the parameter is undefined, not null or undefined. We can’t do both (the default value and the null if block) because the default value could be a function call, for example f = (a = b()) -> a.

CoffeeScript 2 was going to break backward compatibility anyway, but we want to break as little as possible. Is this null/undefined interchangeability something that people value in CoffeeScript 1.x, or would you not miss it if it went away? How important is it that the generated JavaScript for a feature like default function parameters be as ES-like as possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions