GithubHelp home page GithubHelp logo

proposal-logical-default-assignment's Introduction

Logical Default Assignment

A proposal for using the logical assignments ||= and ??= in default assignment in function arguments and destructuring.

Status

Current stage: -1

Champions

Use Cases

Functions

/**
 * With proposal
 */
function example1(arg ??= "some default value") {}
function example2(arg ||= "some default value") {}
var example3 = (arg ??= "some default value") => {}
var example4 = (arg ||= "some default value") => {}

/**
 * Without proposal
 */
function example1(arg) {
  arg = arg ?? "some default value"
}
function example2(arg) {
  arg = arg || "some default value"
}
var example3 = (arg) => {
  arg = arg ?? "some default value"
}
var example4 = (arg) => {
  arg = arg || "some default value"
}

Destructuring

Arrays

/**
 * With proposal
 */
const [arg1 ??= 0] = [null];

const [arg2 ||= 0] = [""];

const [arg3 ||= 0, arg4 ??= arg3] = ["", null];

/**
 * Without proposal
 */
var [_arg1] = [];
const arg1 = _arg1 ?? 0;

var [_arg2] = [];
const arg2 = _arg2 || 0;

const [arg3, arg4] = [""];
const arg3 = _arg3 || 0;
const arg4 = _arg4 || arg3

Objects

/**
 * With proposal
 */
const { test1 ??= 0 } = { test1: null };

const { test2 ||= "default" } = { test2: "" };

/**
 * Without proposal
 */
var { test1: _test1 } = { test1: null };
const test1 = _test1 ?? 0;

var { test2: _test2 } = { test2: "" };
const test2 = _test2 || "default";

Motivation

Similar to the logical assignment proposal, this further minimizes the syntax for well-known patterns such as function options:

function search({
    depth ||= 1,
    limit ||= 1,
   } ??= {}) {
}
search({ depth: 0 }) // depth and limit are locally scoped in the function as 1

Currently, there is no simple, straightforward method for writing synonymous functionality for complicated objects. In fact, the current babel implementation requires reusing the logic from the destructuring transform in babel with a small abstraction in assignment patterns.

The following is a pathological example:

// With proposal
const { data: { courses: oldCourses ??= [] } = {} } = getState();

// Without proposal
const _getState = getState(),
      _getState$data = _getState.data,
      _getState$data2 = _getState$data === void 0 ? {} : _getState$data,
      _getState$data2$cours = _getState$data2.courses,
      oldCourses = _getState$data2$cours ?? [];

Semantics

We defer the reader to the semantics provided in the logical assignment proposal.

References

A babel plugin has been made, but will not be published until this proposal reaches stage 1. For those interested, you can test the proposal on this repo where the plugin resides: https://github.com/jun-sheaf/babel

proposal-logical-default-assignment's People

Contributors

jun-sheaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

proposal-logical-default-assignment's Issues

This proposal has been withdrawn

After much thought, I have decided to withdraw this proposal. In spirit, this proposal provides a nice and convenient syntax for its motivation, but there is a problem: there are no practical use cases.

Basically, there are two key observations:

  1. TypeScript for the most part has removed null for function arguments to the extent that uses of null are generally intentional. Because of this, using ??= there is not very useful.
  2. Using ||= and ??= only helps to allow invalid input to be ignored. This will make code error-prone. For example, take the motivating example of search in the Motivation section. Practically speaking, all the parameters will be validated and only after validation will the value matter. If we allow limit to be 0, then the limit will effectively be ignored and the code will succeed. The catch is the user may believe their passed in limit is positive because a past search succeeded, thus using ??= and ||= will have the developer scrambling for why their limit worked which could lie in some library out of the developer's control.

There is a niche of people who would probably want this feature for object destructuring, but to that, I repeat the second point with a remark: practically speaking, all parameters will be validated and at that time defaults can be set as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.