Skip to content

Instantly share code, notes, and snippets.

@samuelgoto
Last active October 25, 2017 00:05
Show Gist options
  • Save samuelgoto/405251eb4fd7b42d764589418ea2afc4 to your computer and use it in GitHub Desktop.
Save samuelgoto/405251eb4fd7b42d764589418ea2afc4 to your computer and use it in GitHub Desktop.

This is a very early stage 0 exploration to add enums to Javascript, as a syntatic simplication over a common pattern to define enumerations.

Introduction

Enums come up often in code bases (TODO: try to estimate a number) and it is easy to get it incorrectly. Specifically, it is easy to forget:

  • to Object.freeze the object
  • to declare it as a const which avoids having the symbol redefined

With this in mind, we propose a nem keyword to javascript, say enum which de-sugars to the following:

// if you write this ...
enum Foo {
  BAR: 1,
  HELLO "hello",
  WORLD: false
}

// ... it gets de-sugared to:
const Foo = Objec.freeze({
  BAR: 1,
  HELLO: "hello",
  WORLD: false
});

// TODO(goto): should we use Symbols here for extra type safety?
@samuelgoto
Copy link
Author

BigQuery

SELECT
  sample_path,
  sample_repo_name,
  content
FROM [bigquery-public-data:github_repos.sample_contents]
WHERE
  sample_path LIKE '%.js' and
  content LIKE '%@enum%' and
  not content LIKE '%goog%'
  
limit 100;

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