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.

Functions that accept and return objects should respect frozenness

See original GitHub issue

For example, with assoc:

var first = Object.freeze({a: 1, b: 2});
var second = R.assoc('c', 3, first);

console.log(Object.isFrozen(second)); // currently false

I believe that Ramda should maintain the frozenness of objects passed in. Discuss!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kurtmilamcommented, Jul 13, 2017

@matthewp An ESLint rule might help you remain true to your FP principles. It looks like there are multiple options in this area, according to this google search.

That won’t help with libraries that mutate your objects, but it’s a start.

It might be interesting to have a companion library to Ramda that deep-freezes every object returned by any of the library’s functions. I wouldn’t use this in production, and I wouldn’t need it for myself, but it might be useful in a team scenario where not all team members can be trusted to avoid mutation to install the deep-freeze library as a development dependency and have a flag in your app that chooses which version of Ramda to load - deep-freeze for development and vanilla for production.

The excellent partial.lenses library has this behavior baked in. If I understand correctly, it deep freezes all objects returned by its methods if process.env.NODE_ENV is anything other than ‘production’. Everything’s frozen during development, but freezing is turned off in production.

0reactions
kurtmilamcommented, Jul 14, 2017

Most Ramda functions that take objects as arguments and return objects work with the objects’ own enumerable properties. There is no isFrozen own enumerable property on any object unless someone has added it explicitly (as you did to first in your example).

Think of R.assoc as being similar to JavaScript’s native Object.assign. This native method, like R.assoc, only takes into account own enumerable properties.

For example, you could naively recreate R.assoc based on Object.assign:

const assocNative =
  ( propName, propVal, target ) =>
    Object.assign( {}, target, { [ propName ]: propVal } )

The object returned by this assocNative function will always be ‘unfrozen’, regardless of whether the target object passed into it was frozen.

See a more thorough example on Ramda’s REPL.

Also, you may be interested in taking a look at Ramda’s Gitter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object.freeze() - JavaScript - MDN Web Docs
freeze() returns the same object that was passed into the function. It does not create a frozen copy. A TypedArray or a DataView...
Read more >
JavaScript Immutability – Frozen Objects in JS Explained with ...
You can freeze (make immutable) an object using the function Object.freeze(obj) . The object passed to the freeze method will become ...
Read more >
JEP draft: Frozen Arrays (Preview) - OpenJDK
Although the JVM can sometimes do this under the covers, calling "freeze" on a mutable array will always return a different array.
Read more >
The Frozenness of Pseudoclefts - University of Michigan
In order to understand in what ways pseudocleft sentences are restricted in their syntactic turnings, let us take a look at the number...
Read more >
Ruby on Rails v6.0.0.beta1 Release - GitClear
For example these return an array of database config objects for the requested environment and a single database config object will be ...
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