Reputation: 21871
I think burying to the listed languages' source code is too much for me. But is there anyone able to explain in simple terms how does the thing happens?
I mean, in the end immutable data will still be javascript's data. Or is compiled code contains non-std data structures e.g. 'a,b,c' string for immutable array
Upvotes: 3
Views: 411
Reputation: 46218
PureScript has JavaScript's String, Number and Boolean, which are immutable already. On top of that, PureScript has Array and Object but only exposes certain operations.
When you update an Object in PureScript, you're copying the fields except the one you update.
Concatenating Arrays does something like:
function concatArray (xs) {
return function (ys) {
if (xs.length === 0) return ys;
if (ys.length === 0) return xs;
return xs.concat(ys);
};
};
PureScript has extra ways of defining data, these (usually) compile down to Object but also don't expose ways of mutating them.
But using the FFI it is possible to write code which mutates all of your PureScript data. You have to be careful when writing FFI bindings.
Upvotes: 3
Reputation: 1329
I will reply about PureScript as I'm more familiar with this language.
From PureScript By Example [2.7] about PureScript to JavaScript generation:
- Every module gets turned into an object, created by a wrapper function, which contains the module’s exported members.
- PureScript tries to preserve the names of variables wherever possible.
- Function applications in PureScript get turned into function applications in JavaScript.
- The main method is run after all modules have been defined, and is generated as a simple method call with no arguments.
- PureScript code does not rely on any runtime libraries. All of the code that is generated by the compiler originated in a PureScript module somewhere which your code depended on.
These points are important, since they mean that PureScript generates simple, understandable code. In fact, the code generation process in general is quite a shallow transformation. It takes relatively little understanding of the language to predict what JavaScript code will be generated for a particular input.
Therefore, YES as you wrote: "in the end immutable data will still be a javascript's data". After compilation, the immutability is guaranteed at a higher level by the wrapper functions of PureScript modules.
Upvotes: 6