Re: How would we copy... Anything?

2015-02-23 Thread Andreas Rossberg
On 23 February 2015 at 10:37, David Bruant bruan...@gmail.com wrote:

 Le 23/02/2015 10:10, Michał Wadas a écrit :

 Cloning objects is long requested feature.
 clone object javascript yields 1 480 000 results in Google.

 I'd like to share this as an answer
 http://facebook.github.io/immutable-js/#the-case-for-immutability
 If an object is immutable, it can be copied simply by making another
 reference to it instead of copying the entire object. Because a reference
 is much smaller than the object itself, this results in memory savings and
 a potential boost in execution speed for programs which rely on copies
 (such as an undo-stack).

 ```js
 var map1 = Immutable.Map({a:1, b:2, c:3});
 var clone = map1;
 ```


To be clear, that is only true in general as long as immutable objects also
don't have identity. With identity (i.e., reference equality), the
difference between an alias and an actual copy is still observable.
JavaScript is plagued by all objects having identity, a well-known problem
preventing various optimisations in compilers.

In other words, the ES spec would have to make an explicit and conscious
decisions to say that cloning an immutable object is not actually cloning
it. A compiler or runtime system usually cannot apply that decision itself
(except for primitives, which have structural equality).

Despite people *saying* all over the Internet they want cloning, maybe they
 want immutability?


Yeah, I tend to agree with that sentiment.

/Andreas


My proposition is to create a new well known Symbol - Symbol.clone and
 corresponding method on Object - Object.clone.

 Default behavior for an object is to throw on clone try.
 Object.prototype[Symbol.clone] = () = { throw TypeError; }
 Users are encorauged to define their own Symbol.clone logic.

 Primitives are cloned easily.
 Number.prototype[Symbol.clone] = String.prototype[Symbol.clone] =
 Boolean.prototype[Symbol.clone] = function() {return this.valueOf();}

 Primitives are immutable, no need to clone them.
 If you're referring to primitive objects, it might be better to forget
 about this weird corner of the language than polish it.

 Back to something you wrote above:

 Users are encorauged to define their own Symbol.clone logic.

 Perhaps this cloning protocol can be purely implemented in userland as a
 library and doesn't need support from the language. That's one of the
 reasons symbols have been introduced after all.

 David

 ___
 es-discuss mailing list
 es-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es-discuss

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: How would we copy... Anything?

2015-02-23 Thread Anne van Kesteren
On Mon, Feb 23, 2015 at 10:10 AM, Michał Wadas michalwa...@gmail.com wrote:
 My proposition is to create a new well known Symbol - Symbol.clone and
 corresponding method on Object - Object.clone.

Anything that does not deal with
https://github.com/dslomov-chromium/ecmascript-structured-clone should
not be considered.


-- 
https://annevankesteren.nl/
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


How would we copy... Anything?

2015-02-23 Thread Michał Wadas
Cloning objects is long requested feature.
clone object javascript yields 1 480 000 results in Google.

My proposition is to create a new well known Symbol - Symbol.clone and
corresponding method on Object - Object.clone.

Default behavior for an object is to throw on clone try.
Object.prototype[Symbol.clone] = () = { throw TypeError; }
Users are encorauged to define their own Symbol.clone logic.

Primitives are cloned easily.
Number.prototype[Symbol.clone] = String.prototype[Symbol.clone] =
Boolean.prototype[Symbol.clone] = function() {return this.valueOf();}


Sets, Dates, Maps, WeakMaps, WeakSets have cloned their internal state.
Set.prototype[Symbol.clone] = function() {return new Set(this);}
(cloning WeakMap or WeakSet isn't possible in ES6 code anyway)

Every object returned from JSON.parse can be cloned if all it's
subnodes are clonable.

Arrays can be cloned if all it's items are clonable.

Moreover, it can be used as future way to clone iterators.
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: How would we copy... Anything?

2015-02-23 Thread David Bruant

Hi,

Le 23/02/2015 10:10, Michał Wadas a écrit :

Cloning objects is long requested feature.
clone object javascript yields 1 480 000 results in Google.

I'd like to share this as an answer
http://facebook.github.io/immutable-js/#the-case-for-immutability
If an object is immutable, it can be copied simply by making another 
reference to it instead of copying the entire object. Because a 
reference is much smaller than the object itself, this results in memory 
savings and a potential boost in execution speed for programs which rely 
on copies (such as an undo-stack).


```js
var map1 = Immutable.Map({a:1, b:2, c:3});
var clone = map1;
```

Despite people *saying* all over the Internet they want cloning, maybe 
they want immutability?



My proposition is to create a new well known Symbol - Symbol.clone and
corresponding method on Object - Object.clone.

Default behavior for an object is to throw on clone try.
Object.prototype[Symbol.clone] = () = { throw TypeError; }
Users are encorauged to define their own Symbol.clone logic.

Primitives are cloned easily.
Number.prototype[Symbol.clone] = String.prototype[Symbol.clone] =
Boolean.prototype[Symbol.clone] = function() {return this.valueOf();}

Primitives are immutable, no need to clone them.
If you're referring to primitive objects, it might be better to forget 
about this weird corner of the language than polish it.


Back to something you wrote above:

Users are encorauged to define their own Symbol.clone logic.
Perhaps this cloning protocol can be purely implemented in userland as a 
library and doesn't need support from the language. That's one of the 
reasons symbols have been introduced after all.


David
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: How would we copy... Anything?

2015-02-23 Thread Michał Wadas
I'm aware about structured clone, but there is a basic difference -
structured clone is a way to copy object to other Realm, Symbol.clone would
be a more powerful (supporting constructors, classes and functions) way to
clone objects.
On Feb 23, 2015 10:29 AM, Anne van Kesteren ann...@annevk.nl wrote:

 On Mon, Feb 23, 2015 at 10:10 AM, Michał Wadas michalwa...@gmail.com
 wrote:
  My proposition is to create a new well known Symbol - Symbol.clone and
  corresponding method on Object - Object.clone.

 Anything that does not deal with
 https://github.com/dslomov-chromium/ecmascript-structured-clone should
 not be considered.


 --
 https://annevankesteren.nl/

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss