hughfdjackson / immutable

neatly packages immutable equivalents to JavaScript's Objects and Arrays.
101 stars 7 forks source link

Flag to indicate that the object is immutable #29

Closed hughfdjackson closed 11 years ago

hughfdjackson commented 11 years ago

Flag or function to show that an immutable value is immutable:

Suggestions:

var o = im.object()
o.immutable //= true

Pros:

Cons:

var o = im.object()
im.isImmutable(o) //= true

Pros:

Cons:

hughfdjackson commented 11 years ago

For the record, .immutable was preferred because it didn't rely on object identity - which can be a tricky situation when you have a system like NPM that will allow the system to use multiple versions of the same library.

If an immutable object is passed back by library N, then querying it via a directly required instance of immutable should work.

Raynos commented 11 years ago

Why do you need this flag?

hughfdjackson commented 11 years ago

To provide a slightly less duck-typy way to deal with mixed immutable and 'regular' objects - for instance, for a deep get that might (not decided yet) include both mutable and immutable objects:

hughfdjackson commented 11 years ago

and, ofc, because npm breaks instanceof as a universal checker - as you demonstrated so amply before with your prototypes problem.

Raynos commented 11 years ago

Functions that deal with mixed immutable and 'regular' objects are very expensive in terms of additional complexity.

Avoid it if you can.

hughfdjackson commented 11 years ago

You may well be right - still undecided on how deep operations should work. That said, sometimes important to know if something's claiming to be (for instance) a promise, or is some other type of return value.

I think this flag will find its place in making it easy to reason about these objects 'in the wild' (when more than one lib exports them). I'll freely admit it's a slight API gamble; but erring slightly more on the side of making it easy for people to integrate into their daily lives doesn't seem like a step too far.

Raynos commented 11 years ago

I prefer x.type === "immutable@Immutable for that kind of stuff, but sure