Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[all] replace enums with type unions #256

Closed
postspectacular opened this issue Nov 11, 2020 · 1 comment
Closed

[all] replace enums with type unions #256

postspectacular opened this issue Nov 11, 2020 · 1 comment

Comments

@postspectacular
Copy link
Member

postspectacular commented Nov 11, 2020

Various packages in the umbrella repo started out using const enums, which unlike standard enums, would get compiled into numeric constants without any other overhead (i.e. object property lookup). Unfortunately, using const enums would also eventually cause issues with the popular TS isolatedModules build flag (see #154) and so most (if not all) of these enums have already been downgraded to normal enums, with not much benefit...

In many cases, the same type safety can be achieved by using type unions of string constants, also potentially providing better (less verbose) development experience.

enum Type {
  /**
   * Some info about FOO
   */ 
  FOO,
  BAR,
  BAZ
}

// vs
type Type = "foo" | "bar" | "baz";

(an alternative/related approach is shown here)

The only potential downside is the loss of docstrings for individual enum values, but that can be addressed at type level... autocomplete and typesafety won't be impacted, though. Would also be good to do some benchmarking before updating the code.

Currently, there're ~30 uses of enums in the repo, not all of them qualify for the above replacement, but many do.

For example, the following examples (not exhaustive) DO NOT qualify, since they rely on their specific numeric enum values:

@nkint
Copy link
Contributor

nkint commented Nov 11, 2020

I am a big supporter of string versus enum, so.. 🎉

postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace ColorMode w/ type alias
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace filter type enums w/ type aliases

- FilterType
- BiquadType
- SVFType
- OnepoleType
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace GeneType w/ type alias
postspectacular added a commit that referenced this issue Dec 22, 2020
- update comparison order in classifyCrossing()

BREAKING CHANGE: replace Crossing enum w/ type alias
postspectacular added a commit that referenced this issue Dec 22, 2020
- replace NodeType enum w/ type alias
- update grammar & compiler
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace GestureType w/ type alias
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace Type enum w/ type alias, update BinStructItem
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace SegmentType enum w/ type alias
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace SegmentType w/ type alias
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: remove obsolete shape Type enum
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace Type enum returned by IShape.type w/ string consts

- update all shape classes
- update all ops/multimethod dispatches
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace Patch enum w/ type alias,
update PatchArrayOp/PatchObjOp
postspectacular added a commit that referenced this issue Dec 22, 2020
BREAKING CHANGE: replace DiffMode enum w/ type alias

- rename DiffMode.ONLY_DISTANCE_LINEAR_ONLY_CHANGES => "minimal"
- update diffObject() mode arg to only allow: "full" or "only-distance"
postspectacular added a commit that referenced this issue Dec 22, 2020
* feature/256-enum-refactor:
  docs(color): update gradient preview gen
  docs: update readmes
  refactor(hdom-canvas): update DiffMode handling
  refactor(hdom): update DiffMode handling
  refactor(hdiff): update DiffMode handling
  refactor(diff): fix #256 replace DiffMode enum
  refactor(transducers-patch): fix #256 replace enum
  refactor(geom): fix #256 replace Type enum w/ alias
  refactor(geom-api): fix #256 remove Type enum
  refactor(examples): update GestureType usage
  refactor(geom): fix #256 replace enum w/ type alias
  refactor(geom-api): fix #256 replace enum w/ type alias
  refactor(transducers-binary): fix #256 replace enum w/ type alias
  refactor(rstream-gestures): fix #256 replace GestureType enum
  refactor(pointfree-lang): fix #256 replace enum w/ type alias
  fix(math): fix #256 replace enum w/ type alias
  refactor(gp): fix #256 replace enum w/ type alias
  refactor(adjacency): fix #256 replace enums w/ type aliases
  refactor(color): fix #256 replace enum w/ type alias
  refactor(adjacency): fix #256 replace enum w/ type alias
postspectacular added a commit that referenced this issue Mar 16, 2021
- replace `Filter` & `Wrap` enums w/ type aliases (#256)
- add HOF `defSampler()` w/ impls for PackedBuffer only (so far)
- add `resize()` (also PackedBuffer only)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants