-
Notifications
You must be signed in to change notification settings - Fork 2
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
Immutable API? #1
Labels
Comments
deathcap
added a commit
that referenced
this issue
Dec 30, 2013
This is a major incompatible API change. Experimental (see GH-1) but all tests pass. Changes include: - instead of mutating this, methods now return a new object - some methods now return multiple values (array); first is a new version of the same object, second element is the other value - methods renamed to reflect immutability (increase -> increased, etc.) - removed clone(), since you don't need to clone immutable objects - 'undefined' is now consistently used to represent 'no item', and is supported both as input parameters and output return values - piles must always have a positive count; 0 (or negative) is now illegal, also item names must always be defined - item, count, tags are all read-only properties, and everything is frozen - no more bugs from invalid states
Added on branch 'immutable', but not sure if I'll use it (an enjoyable experiment nonetheless) |
deathcap
added a commit
that referenced
this issue
Dec 30, 2013
deathcap
added a commit
to deathcap/inventory-window
that referenced
this issue
Dec 30, 2013
Normally should not exist, but sometimes do for merging purposes. Render these instead of hiding them to help diagnose issues whenever they crop up. Related deathcap/itempile#1
deathcap
added a commit
to deathcap/inventory-window
that referenced
this issue
Dec 31, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Immutability has many benefits, making it easier to reason about state avoiding certain classes of bugs. Noteworthy for itempile:
However, this convention requires special-cases empty slots accordingly, on both input (checking for undefined) and output (setting undefined if an operation empties a slot). If itempiles were immutable, instead of mutating in-place (requiring the caller to check in/out for undefined), operations would return a new object, which could either be an itempile instance or undefined, therefore simplifying and centralizing the "no item" handling logic.
This is why 0.0.3 added clone(), to create a new pile with the same item, count, tags, yet a distinct object that can be modified independently. Currently itempile clone is only used in craftingrecipes computeOutput(), since it returns a new pile based on a recipe index.
The clone() implementation in itempile 0.0.3 is actually wrong, since it doesn't clone the tags (edit: fixed in 1ea6fd1). Tags can be arbitrarily nested so I'm going to need a "deep clone" to properly fix this.
Or if everything was immutable, no cloning would be needed since nothing could change.
Downsides of immutability:
In CoffeeScript this is painless with [a2, b] = a.mergedPile(b), destructuring assignment, but is much more cumbersome in current widely supported JavaScript implementations. Firefox 2 with JavaScript 1.7 added destructuring assignment, and supposedly it is slated for ECMAScript 6, but the current stable version of Chrome (31.0.1650.63) doesn't support it. This means you have to write something like: var array = a.mergedPile(b); var a2 = array[0], b = array[1], not pleasant at all. It is only syntax, but syntax matters, still a pain.
In summary, immutability for this module may or may not be worth it. Definitely an interesting idea though.
The text was updated successfully, but these errors were encountered: