GithubHelp home page GithubHelp logo

Comments (9)

alanhogan avatar alanhogan commented on July 20, 2024

Is it undesirable or impossible for immutable data structures to handle cycles?

from seamless-immutable.

alanhogan avatar alanhogan commented on July 20, 2024

A better question is: why do you think it is unwise to support circular references? I am merely curious, of course

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 20, 2024

Just that it's time-consuming both from an implementation and from a performance perspective. It would entail building up an intermediate data structure just to track where things need to end up, then converting that back into a cloned object with references appropriately reconnected...

This came up because I accidentally called Immutable on a React component (which contained a circular reference), and it just threw a stack overflow, which made it harder to figure out what I'd done wrong. As soon as I realized the root cause, it was easy to fix, so having a check that results in an exception is all I need to avoid that particular issue in the future. 😄

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 20, 2024

That said, if it's something someone actually needs, I'm not aware of any reason it couldn't be done.

from seamless-immutable.

alanhogan avatar alanhogan commented on July 20, 2024

Yep, makes total sense. seems smart to me

from seamless-immutable.

crudh avatar crudh commented on July 20, 2024

Now that there is a dev/prod mode maybe it would make sense to check this only in development mode? That way most cases can be found without sacrificing any performance.

A really simple way would be to do a test before making it immutable, like:
JSON.stringify(object);

If it contains a circular reference it will throw a TypeError with the message "Converting circular structure to JSON".

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 20, 2024

Great point! It could also then console.error(object) so you could actually see the problematic object in question.

from seamless-immutable.

vegetabill avatar vegetabill commented on July 20, 2024

FYI I was creating an immutable copy of a Backbone object that was using the backbone-associations add-on and ran into this. immutablejs handles them fine but I liked the sound of this library for all the reasons mentioned in your blog: http://noredinktech.tumblr.com/post/107617838018/switching-from-immutable-js-to-seamless-immutable

from seamless-immutable.

rtfeldman avatar rtfeldman commented on July 20, 2024

Resolved by #119

from seamless-immutable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.