Comments (11)
I considered it, but the trouble is that doing this can mask bugs. For example:
function foo() {
var array = [3, 1, 4];
array.sort()
return array;
}
If you change that first line to be Immutable([3, 1, 4])
, what should happen?
Currently array.sort()
throws an exception, letting you know that some code that made sense with a mutable array no longer makes sense. If instead array.sort()
just returns a sorted, immutable array, then now an unsorted array will be returned here, and you have a bug to track down - a potentially nasty one, if there is a lot of logic surrounding this code.
I prefer this API because you can take an immutable array and write any number of helper functions that return sorted ones, but the only way to make bugs like this easy to find is to override the mutable methods to throw an exception. π
from seamless-immutable.
You can add method sorted(fn)
that returns sorted copy of array.
from seamless-immutable.
Great explanation. Richard, youβre an A+ maintainer.
from seamless-immutable.
Thanks for the explanation. It makes sense :). So how are you going about those when you're using methods like sort
? Are you doing something like this?
let array = Immutable([3, 1, 4])
array = Immutable(array.asMutable().sort())
from seamless-immutable.
Thanks @alanhogan! π
@dariocravero Generally I'm using Underscore functions like _.sortBy and just calling Immutable
on the result. No need to call .asMutable()
in that case!
from seamless-immutable.
Good stuff :) Will give it a go then!
from seamless-immutable.
What about an option for whether an error should be thrown? I am appreciative of the thrown error as I change from ImmutableJS to this library. But after the transition, my code is cluttered using _.sortBy()
instead of the built-in sort()
.
While the workaround is not terrible, I thought I'd voice my opinion so others who find this issue know there's at least 1 other person wishing for it!
from seamless-immutable.
@tylercollier Good news! πΈ https://github.com/rtfeldman/seamless-immutable#performance
from seamless-immutable.
Thanks for the link to that section of the doc, which I read (and admit to not reading before, but you already had me sold on performance from your blog post :-) I think you saw my original comment, which was phrased as an option that I might toggle one way in dev and another in production, but I rephrased it because that's not what I'm really after. What I'd like is an option I can set. My concern here is convenience for me as a developer rather than performance. I'm guessing you have a build step that pulls those checks out when you create a minified production build, rather than an option I can set?
from seamless-immutable.
Ahh got it, yeah that's correct. Sorry for the confusion!
That said, I'd honestly rather not add that complication to dev mode. :x
from seamless-immutable.
I like the idea of introducing additional methods to support the same functionality as original mutation methods, but have them return an immutable result instead as @nin-jin suggested.
Maybe even more obvious like sort_immutable()
so you should know what to expect from it?
This would remove the need for custom functions doing the same thing but requiring an additional dependency in your code.
from seamless-immutable.
Related Issues (20)
- Make `without` support removing item from an array HOT 4
- how to use splice like array HOT 1
- Internet Explorer tests are failing HOT 4
- Issue regarding null values HOT 5
- Why are IE tests failing? HOT 3
- How to apply splice with seamless-immutable? HOT 1
- Merge does not work with class instances
- __immutable_invariants_hold is enumerable on Edge HOT 1
- Garbage collector features? HOT 1
- Performance characteristics
- Any difference with deepCopy? HOT 1
- [types] Mixin not applied to nested objects HOT 1
- flatMap not working HOT 2
- Add documentation for merge mode
- Files not being replaced on a deep merge
- Fails on IE 11 HOT 1
- TypeError: Object(...) is not a function HOT 1
- Lodash Vulnerability Problem
- Is the project still maintained? HOT 2
- immutable object causes rapid memory growth HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from seamless-immutable.