question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any interest in allowing mutable methods to return copies?

See original GitHub issue

I’ve moved some bits around in a local clone of the repo so that rather than throwing an exception when any of the banned mutable methods are called, a quick copy is created and is mutated then returned as immutable.

Most of this logic happens in a new function.

function proxyMutableMethod(obj, methodName) {                                                                                                   
   var methodRef = obj[methodName];                                                                                                               

  addPropertyTo(obj, methodName, function() {                                                                                                    
    var host = obj instanceof Array ? [] : {},                                                                                                   
         copy = quickCopy(obj, host);

    methodRef.apply(copy, arguments);
    return Immutable(copy);
  });
}

Rather than calling banProperty for each of the banned methods, proxyMutableMethod is called instead.

Now aside from the obvious performance implications (all these operations become at least O(n)*) is there a reason why this isn’t already a feature of the library? I understand that the performance itself may be enough of a reason, but it makes it more friendly for people coming from Clojure or Immutable.js. Just want to know whether I’m missing something before I crack on with writing the tests and amending the docs so I can send a pull request.

* It wouldn’t be too much work to add an interface for defining structural sharing overrides for some of the mutable methods though.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sudhakarcommented, Oct 30, 2015

👍 Started with seamless-immutable & after failing to easily splice, tried porting it to immutable-js. Now my app has become even more complex with all that get, setIn etc, I am now back here.

How about ruby sort of suffix for immutable methods like splice$ or splice_ ?

0reactions
rtfeldmancommented, Jan 9, 2016

@wesleytodd Instead of doing a = Immutable(a.asMutable().push(val)), I would do a variant of what @jokeyrhyme suggested:

a = Immutable(a).concat([4])

The main reason I went with the “explode when you try to mutate” design was to avoid pernicious bugs when changing over previously mutable code to use Immutable. This way if you ever forget to change something over from the old style to the new style, you’ll at least get an error!

As far as adding a separate set of methods to add this functionality, I like keeping the API intentionally simple, and I don’t think something like Immutable(a).afterPush(4) is enough added convenience over Immutable(a).concat([4]) to justify adding it.

Thanks for the spirited discussion! You folks are excellent. 😺

Read more comments on GitHub >

github_iconTop Results From Across the Web

When does -copy return a mutable object? - Stack Overflow
Copying an NSMutableArray may return an NSMutableArray , since that's the original class, but copying any arbitrary NSArray instance would not.
Read more >
Add syntactic support for mutable return values ... - Swift Forums
I am interested to find what people think of potentially introducing the ability to specify on a function that it can return a...
Read more >
Copying and Mutability | Optimizing Objective-C - InformIT
If you keep sending -copy messages to a mutable object, however, you'll keep getting new copies. This approach is a lot more expensive,...
Read more >
CWE-375: Returning a Mutable Object to an Untrusted Caller
In situations where functions return references to mutable data, it is possible that the external code which called the function may make changes...
Read more >
Object Mutability - Apple Developer
The invoker of a method is interested in the mutability of a returned object for two reasons: It wants to know if it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found