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

Errors Not Thrown on Array.Push #25

Open
andrewjmead opened this issue Mar 30, 2016 · 6 comments
Open

Errors Not Thrown on Array.Push #25

andrewjmead opened this issue Mar 30, 2016 · 6 comments

Comments

@andrewjmead
Copy link

First, thanks for the library. I love that it throws error so I can watch my tests fail.

I'm having an issue with the following snippet:

var arr = immu([])
arr.push('one'); // Does not throw an error (array does not update)

var obj = immu({});
obj.key = 'val'; // Throws an error (object does not update)

Trying to mutate an object runs as expected. The object does not get updated and an error is thrown.

Trying to mutate an array doesn't update the array, but it doesn't throw an error.

Is this expected behavior? Thanks!

@scottcorgan
Copy link
Owner

.push() doesn't actually throw an error because it aims to replicate the functionality of regular Javascript objects as much as possible. Here, .push() will add an it to the end of the array and return a new array (see the implementation here).

But, you make a could point. Should methods that mutate throw errors or should the perform the action in an immutable way?

@andrewjmead
Copy link
Author

Thanks for the information!

I was not aware that a new implementation was being added for array methods that would otherwise update the array. I probably understood the goal of the library. I was looking for a library I could use during tests to make sure my Redux reducers didn't change the state or action argument passed in.

But, you make a could point. Should methods that mutate throw errors or should the perform the action in an immutable way?

Personally, I wouldn't use a library that changes the behavior of the JavaScript methods. I'd rather use the proper method (ES6 spread or .concat()) and avoid bad habits.

What could be useful is error messages that explain why it's mutating the object. For example, calling .push on an "immued" object would throw an error that directs the user towards the correct solution such as concat.

@scottcorgan
Copy link
Owner

You make very interesting points.

I think the original aim for the library was to mimic the Javascript api for objects and arrays, and just throw helpful errors when the user tries to mutate anything. In that spirit, all the methods were changed to be immutable if they were original mutable.

I'm open to starting a new library that just throws errors on use of any mutation and leaves the methods as-is.

Thoughts?

@andrewjmead
Copy link
Author

Hey @scottcorgan.

I think both are useful. I originally used deep-freeze which prevents modifications but doesn't throw errors when a modification is attempted.

I then found deep-freeze-strict which will throw errors in my tests if the object in question gets modified.

@ajhyndman
Copy link

I sympathize with @andrewjmead's suggestion here.

I actually have been searching for a minimal, well-supported way to guarantee immutability while using something completely functional (like Ramda) for modification.

Thanks for putting this library together, and sharing it!

@ghost
Copy link

ghost commented Nov 10, 2016

Yes, I really am looking for a lib that just properly freezies everything, removes mutators and that's it. I don't want to be coupled to a immutability library because someone relies on using their mutator methods.

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

3 participants