Skip to content

Instantly share code, notes, and snippets.

@komu
Created August 25, 2023 05:19
Show Gist options
  • Save komu/3781b85c8adf46772421e57826a38ef9 to your computer and use it in GitHub Desktop.
Save komu/3781b85c8adf46772421e57826a38ef9 to your computer and use it in GitHub Desktop.

Proposal for Introducing nil to ECMAScript

Abstract

This proposal aims to introduce a new primitive value nil to the ECMAScript language, addressing a gap between null and undefined. While seemingly unnecessary, this addition offers subtle and nuanced differentiation in handling non-values, aligning with modern programming paradigms.

Specification

  • Value: nil
  • Type: typeof nil returns "absent"
  • Comparison:
    • nil == null (loose equality)
    • nil !== undefined
    • nil === nil
  • Default Parameter Value: Functions can have a default value of nil, distinct from undefined, allowing differentiation between omitted parameters and those explicitly passed as undefined.

Syntax

let x = nil; // x is now "absent"

Example Usage

function greet(name = nil) {
  if (name === nil) {
    return "Name is absent, not undefined!";
  }
  return `Hello, ${name}!`;
}

Semantics

nil introduces an intermediate state between the non-existence represented by undefined and the intentional absence indicated by null. This facilitates a tri-state logic for handling parameters and object properties, enriching the semantics of the language.

Conversion Rules

  • JSON.stringify will convert nil to "nil"
  • When converted to a boolean, nil will evaluate to false

Backward Compatibility

The addition of nil will not affect existing code as it introduces a new primitive value, distinct from null and undefined.

Potential Concerns and Counterarguments

  • Redundancy with null and undefined: The introduction of nil provides more granular control over handling non-values and aligns with emerging programming practices that distinguish between different types of absence.

  • Learning Curve: While this may add complexity, the differentiation between nil, null, and undefined enables more expressive coding patterns.

Conclusion

The introduction of nil into ECMAScript is a logical evolution that bridges the semantic gap between null and undefined. By embracing a more nuanced understanding of absence, this proposal enhances the language's flexibility and expressiveness, making it even more aligned with the complex needs of modern software development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment