Refinements.
(duplicate post)
Hi everyone. I have a proposal for ES6. I don't know if it's been discussed before, so if it has, I don't mean to waste your time … hopefully, I'll be directed to the right place. One of the most debated JavaScript topics (at least in my experience) is whether or not one should modify objects that don't belong to them ... some say it's okay if you "know what you're doing" and others see it as a crime. I propose we solve this problem by adding "refinements" to ES6. Ruby v2.0.0 introduced a nice little feature dubbed "refinements". Refinements allow you to extend other objects for specific modules of code. I know this isn't a Ruby mailing list, but because the inspiration comes from the Ruby implementation, an example is in order (feel free to ignore it). Say you want to modify the `to_str()` method in the built-in String class in Ruby, the common way to do this is as follows. ```ruby class String def to_str() "Blah, blah, blah ... this is an example." end end ``` Very simple ... just re-open the class and create a method with the same name. However, there's a problem with this, which happens to be the same problem we have in JavaScript, and that's the fact that we've now just modified this method permanently for the rest of the execution. Well, with refinements, the better way of accomplishing the same thing is as follows. ```ruby module MyLibrary refine(String) do def to_str() "Blah, blah, blah ... this is an example." end end end ``` Now, if you try to send `to_str()` to, say, a string object `"hello"` (`"hello".to_str()`), you'll get the original `"hello"` as a return value. In order to use the refinements made to the class, you have to do the following. ```ruby using MyLibrary puts("hello".to_str()) ``` Running the code above properly outputs the string `Blah, blah, blah ... this is an example.`. Refinements can only be used after a `using` statement and only within that file. Given that we're already getting modules in ES6, I believe something similar to this would be a great addition to the language. I can definitely work on a more in-depth and detailed proposal, with specific JavaScript examples, if needed, but I just would like to hear thoughts around the idea. Thanks! - Jonathan Barronville @jonathanmarvens Sent with Airmail. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131020/8752c3d4/attachment.html>
(duplicate post)
Hi everyone. I have a proposal for ES6. I don't know if it's been discussed before, so if it has, I don't mean to waste your time … hopefully, I'll be directed to the right place. One of the most debated JavaScript topics (at least in my experience) is whether or not one should modify objects that don't belong to them ... some say it's okay if you "know what you're doing" and others see it as a crime. I propose we solve this problem by adding "refinements" to ES6. Ruby v2.0.0 introduced a nice little feature dubbed "refinements". Refinements allow you to extend other objects for specific modules of code. I know this isn't a Ruby mailing list, but because the inspiration comes from the Ruby implementation, an example is in order (feel free to ignore it). Say you want to modify the `to_str()` method in the built-in String class in Ruby, the common way to do this is as follows. ```ruby class String def to_str() "Blah, blah, blah ... this is an example." end end ``` Very simple ... just re-open the class and create a method with the same name. However, there's a problem with this, which happens to be the same problem we have in JavaScript, and that's the fact that we've now just modified this method permanently for the rest of the execution. Well, with refinements, the better way of accomplishing the same thing is as follows. ```ruby module MyLibrary refine(String) do def to_str() "Blah, blah, blah ... this is an example." end end end ``` Now, if you try to send `to_str()` to, say, a string object `"hello"` (`"hello".to_str()`), you'll get the original `"hello"` as a return value. In order to use the refinements made to the class, you have to do the following. ```ruby using MyLibrary puts("hello".to_str()) ``` Running the code above properly outputs the string `Blah, blah, blah ... this is an example.`. Refinements can only be used after a `using` statement and only within that file. Given that we're already getting modules in ES6, I believe something similar to this would be a great addition to the language. I can definitely work on a more in-depth and detailed proposal, with specific JavaScript examples, if needed, but I just would like to hear thoughts around the idea. Thanks! - Jonathan Barronville @jonathanmarvens -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131020/2495d3e8/attachment.html>
I think this idea is certainly worth writing a strawman for, but it's too late in the ES6 cycle and this couldn't even be considered until ES7. Would you be willing to write up a "Refinements" strawman with some proposed ES-centric syntax and some preliminary semantics sketched?
On Sun, Oct 20, 2013 at 9:01 AM, Jonathan Barronville < jonathan at belairlabs.com> wrote: > Hi everyone. > > I have a proposal for ES6. I don't know if it's been discussed before, so > if it has, I don't mean to waste your time … hopefully, I'll be directed to > the right place. > > One of the most debated JavaScript topics (at least in my experience) is > whether or not one should modify objects that don't belong to them ... some > say it's okay if you "know what you're doing" and others see it as a crime. > > I propose we solve this problem by adding "refinements" to ES6. Ruby > v2.0.0 introduced a nice little feature dubbed "refinements". Refinements > allow you to extend other objects for specific modules of code. I know this > isn't a Ruby mailing list, but because the inspiration comes from the Ruby > implementation, an example is in order (feel free to ignore it). > > Say you want to modify the `to_str()` method in the built-in String class > in Ruby, the common way to do this is as follows. > > ```ruby > class String > def to_str() > "Blah, blah, blah ... this is an example." > end > end > ``` > > Very simple ... just re-open the class and create a method with the same > name. However, there's a problem with this, which happens to be the same > problem we have in JavaScript, and that's the fact that we've now just > modified this method permanently for the rest of the execution. > > Well, with refinements, the better way of accomplishing the same thing is > as follows. > > ```ruby > module MyLibrary > refine(String) do > def to_str() > "Blah, blah, blah ... this is an example." > end > end > end > ``` > > Now, if you try to send `to_str()` to, say, a string object `"hello"` > (`"hello".to_str()`), you'll get the original `"hello"` as a return value. > In order to use the refinements made to the class, you have to do the > following. > > ```ruby > using MyLibrary > > puts("hello".to_str()) > ``` > > Running the code above properly outputs the string `Blah, blah, blah ... > this is an example.`. > > Refinements can only be used after a `using` statement and only within > that file. > > Given that we're already getting modules in ES6, I believe something > similar to this would be a great addition to the language. > > I can definitely work on a more in-depth and detailed proposal, with > specific JavaScript examples, if needed, but I just would like to hear > thoughts around the idea. > > Thanks! > I think this idea is certainly worth writing a strawman for, but it's too late in the ES6 cycle and this couldn't even be considered until ES7. Would you be willing to write up a "Refinements" strawman with some proposed ES-centric syntax and some preliminary semantics sketched? Rick -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131021/06a6c1f4/attachment.html>
Sure thing Rick, I will definitely do that.
I'd love to hear any current thoughts and feedback around the idea though.
Sure thing Rick, I will definitely do that. I'd love to hear any current thoughts and feedback around the idea though. Thanks! - Jonathan Barronville @jonathanmarvens On October 21, 2013 at 1:26:57 AM, Rick Waldron (waldron.rick at gmail.com) wrote: On Sun, Oct 20, 2013 at 9:01 AM, Jonathan Barronville <jonathan at belairlabs.com> wrote: Hi everyone. I have a proposal for ES6. I don't know if it's been discussed before, so if it has, I don't mean to waste your time … hopefully, I'll be directed to the right place. One of the most debated JavaScript topics (at least in my experience) is whether or not one should modify objects that don't belong to them ... some say it's okay if you "know what you're doing" and others see it as a crime. I propose we solve this problem by adding "refinements" to ES6. Ruby v2.0.0 introduced a nice little feature dubbed "refinements". Refinements allow you to extend other objects for specific modules of code. I know this isn't a Ruby mailing list, but because the inspiration comes from the Ruby implementation, an example is in order (feel free to ignore it). Say you want to modify the `to_str()` method in the built-in String class in Ruby, the common way to do this is as follows. ```ruby class String def to_str() "Blah, blah, blah ... this is an example." end end ``` Very simple ... just re-open the class and create a method with the same name. However, there's a problem with this, which happens to be the same problem we have in JavaScript, and that's the fact that we've now just modified this method permanently for the rest of the execution. Well, with refinements, the better way of accomplishing the same thing is as follows. ```ruby module MyLibrary refine(String) do def to_str() "Blah, blah, blah ... this is an example." end end end ``` Now, if you try to send `to_str()` to, say, a string object `"hello"` (`"hello".to_str()`), you'll get the original `"hello"` as a return value. In order to use the refinements made to the class, you have to do the following. ```ruby using MyLibrary puts("hello".to_str()) ``` Running the code above properly outputs the string `Blah, blah, blah ... this is an example.`. Refinements can only be used after a `using` statement and only within that file. Given that we're already getting modules in ES6, I believe something similar to this would be a great addition to the language. I can definitely work on a more in-depth and detailed proposal, with specific JavaScript examples, if needed, but I just would like to hear thoughts around the idea. Thanks! I think this idea is certainly worth writing a strawman for, but it's too late in the ES6 cycle and this couldn't even be considered until ES7. Would you be willing to write up a "Refinements" strawman with some proposed ES-centric syntax and some preliminary semantics sketched? Rick -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131021/8aa633c3/attachment-0001.html>
There is another thread covering this exact topic at the moment. See "Scooped binding of a method to an object".
There is another thread covering this exact topic at the moment. See "Scooped binding of a method to an object". On Oct 21, 2013 12:49 AM, "Jonathan Barronville" <jonathan at belairlabs.com> wrote: > > Hi everyone. > > I have a proposal for ES6. I don't know if it's been discussed before, so if it has, I don't mean to waste your time … hopefully, I'll be directed to the right place. > > One of the most debated JavaScript topics (at least in my experience) is whether or not one should modify objects that don't belong to them ... some say it's okay if you "know what you're doing" and others see it as a crime. > > I propose we solve this problem by adding "refinements" to ES6. Ruby v2.0.0 introduced a nice little feature dubbed "refinements". Refinements allow you to extend other objects for specific modules of code. I know this isn't a Ruby mailing list, but because the inspiration comes from the Ruby implementation, an example is in order (feel free to ignore it). > > Say you want to modify the `to_str()` method in the built-in String class in Ruby, the common way to do this is as follows. > > ```ruby > class String > def to_str() > "Blah, blah, blah ... this is an example." > end > end > ``` > > Very simple ... just re-open the class and create a method with the same name. However, there's a problem with this, which happens to be the same problem we have in JavaScript, and that's the fact that we've now just modified this method permanently for the rest of the execution. > > Well, with refinements, the better way of accomplishing the same thing is as follows. > > ```ruby > module MyLibrary > refine(String) do > def to_str() > "Blah, blah, blah ... this is an example." > end > end > end > ``` > > Now, if you try to send `to_str()` to, say, a string object `"hello"` (`"hello".to_str()`), you'll get the original `"hello"` as a return value. In order to use the refinements made to the class, you have to do the following. > > ```ruby > using MyLibrary > > puts("hello".to_str()) > ``` > > Running the code above properly outputs the string `Blah, blah, blah ... this is an example.`. > > Refinements can only be used after a `using` statement and only within that file. > > Given that we're already getting modules in ES6, I believe something similar to this would be a great addition to the language. > > I can definitely work on a more in-depth and detailed proposal, with specific JavaScript examples, if needed, but I just would like to hear thoughts around the idea. > > Thanks! > > - Jonathan Barronville > @jonathanmarvens > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131021/cc37de5b/attachment.html>
Can you clarify the overlap?
On Mon, Oct 21, 2013 at 9:16 AM, Erik Arvidsson <erik.arvidsson at gmail.com>wrote: > There is another thread covering this exact topic at the moment. See > "Scooped binding of a method to an object". > Can you clarify the overlap? Thanks! Rick > On Oct 21, 2013 12:49 AM, "Jonathan Barronville" <jonathan at belairlabs.com> > wrote: > > > > Hi everyone. > > > > I have a proposal for ES6. I don't know if it's been discussed before, > so if it has, I don't mean to waste your time … hopefully, I'll be directed > to the right place. > > > > One of the most debated JavaScript topics (at least in my experience) is > whether or not one should modify objects that don't belong to them ... some > say it's okay if you "know what you're doing" and others see it as a crime. > > > > I propose we solve this problem by adding "refinements" to ES6. Ruby > v2.0.0 introduced a nice little feature dubbed "refinements". Refinements > allow you to extend other objects for specific modules of code. I know this > isn't a Ruby mailing list, but because the inspiration comes from the Ruby > implementation, an example is in order (feel free to ignore it). > > > > Say you want to modify the `to_str()` method in the built-in String > class in Ruby, the common way to do this is as follows. > > > > ```ruby > > class String > > def to_str() > > "Blah, blah, blah ... this is an example." > > end > > end > > ``` > > > > Very simple ... just re-open the class and create a method with the same > name. However, there's a problem with this, which happens to be the same > problem we have in JavaScript, and that's the fact that we've now just > modified this method permanently for the rest of the execution. > > > > Well, with refinements, the better way of accomplishing the same thing > is as follows. > > > > ```ruby > > module MyLibrary > > refine(String) do > > def to_str() > > "Blah, blah, blah ... this is an example." > > end > > end > > end > > ``` > > > > Now, if you try to send `to_str()` to, say, a string object `"hello"` > (`"hello".to_str()`), you'll get the original `"hello"` as a return value. > In order to use the refinements made to the class, you have to do the > following. > > > > ```ruby > > using MyLibrary > > > > puts("hello".to_str()) > > ``` > > > > Running the code above properly outputs the string `Blah, blah, blah ... > this is an example.`. > > > > Refinements can only be used after a `using` statement and only within > that file. > > > > Given that we're already getting modules in ES6, I believe something > similar to this would be a great addition to the language. > > > > I can definitely work on a more in-depth and detailed proposal, with > specific JavaScript examples, if needed, but I just would like to hear > thoughts around the idea. > > > > Thanks! > > > > - Jonathan Barronville > > @jonathanmarvens > > > > _______________________________________________ > > es-discuss mailing list > > es-discuss at mozilla.org > > https://mail.mozilla.org/listinfo/es-discuss > > > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131021/fdbbc598/attachment.html>
The overlap appears to be 100% - they're the exact same concept. It's just different words for "add something to this object within this context, but not outside".
On Mon, Oct 21, 2013 at 8:19 AM, Rick Waldron <waldron.rick at gmail.com> wrote: > On Mon, Oct 21, 2013 at 9:16 AM, Erik Arvidsson <erik.arvidsson at gmail.com> > wrote: >> There is another thread covering this exact topic at the moment. See >> "Scoped binding of a method to an object". > > Can you clarify the overlap? Thanks! The overlap appears to be 100% - they're the exact same concept. It's just different words for "add something to this object within this context, but not outside". ~TJ
When we proposed scoped object extensions we looked at Ruby's refinements.
When we proposed scoped object extensions we looked at Ruby's refinements. On Mon, Oct 21, 2013 at 11:25 AM, Tab Atkins Jr. <jackalmage at gmail.com> wrote: > On Mon, Oct 21, 2013 at 8:19 AM, Rick Waldron <waldron.rick at gmail.com> wrote: >> On Mon, Oct 21, 2013 at 9:16 AM, Erik Arvidsson <erik.arvidsson at gmail.com> >> wrote: >>> There is another thread covering this exact topic at the moment. See >>> "Scoped binding of a method to an object". >> >> Can you clarify the overlap? Thanks! > > The overlap appears to be 100% - they're the exact same concept. It's > just different words for "add something to this object within this > context, but not outside". > > ~TJ -- erik
Which were inspired by the classbox work that built on Smalltalk classes.
Which were inspired by the classbox work that built on Smalltalk classes. /be On Oct 21, 2013, at 8:29 AM, Erik Arvidsson <erik.arvidsson at gmail.com> wrote: > When we proposed scoped object extensions we looked at Ruby's refinements. > > On Mon, Oct 21, 2013 at 11:25 AM, Tab Atkins Jr. <jackalmage at gmail.com> wrote: >> On Mon, Oct 21, 2013 at 8:19 AM, Rick Waldron <waldron.rick at gmail.com> wrote: >>> On Mon, Oct 21, 2013 at 9:16 AM, Erik Arvidsson <erik.arvidsson at gmail.com> >>> wrote: >>>> There is another thread covering this exact topic at the moment. See >>>> "Scoped binding of a method to an object". >>> >>> Can you clarify the overlap? Thanks! >> >> The overlap appears to be 100% - they're the exact same concept. It's >> just different words for "add something to this object within this >> context, but not outside". >> >> ~TJ > > > > -- > erik > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss
On Mon, Oct 21, 2013 at 11:25 AM, Tab Atkins Jr. <jackalmage at gmail.com>wrote:
The overlap appears to be 100% - they're the exact same concept. It's just different words for "add something to this object within this context, but not outside".
Indeed. I had forgotten about this: strawman:scoped_object_extensions and was mistakenly thinking of the "scoped binding of a method to an object" discussion—which does have overlap, but certainly not aligned 1-to-1 as refinements and scoped object extensions. Sorry for the noise.
On Mon, Oct 21, 2013 at 11:25 AM, Tab Atkins Jr. <jackalmage at gmail.com>wrote: > On Mon, Oct 21, 2013 at 8:19 AM, Rick Waldron <waldron.rick at gmail.com> > wrote: > > On Mon, Oct 21, 2013 at 9:16 AM, Erik Arvidsson < > erik.arvidsson at gmail.com> > > wrote: > >> There is another thread covering this exact topic at the moment. See > >> "Scoped binding of a method to an object". > > > > Can you clarify the overlap? Thanks! > > The overlap appears to be 100% - they're the exact same concept. It's > just different words for "add something to this object within this > context, but not outside". > > Indeed. I had forgotten about this: http://wiki.ecmascript.org/doku.php?id=strawman:scoped_object_extensionsand was mistakenly thinking of the "scoped binding of a method to an object"[0] discussion—which does have overlap, but certainly not aligned 1-to-1 as refinements and scoped object extensions. Sorry for the noise. Rick [0] https://mail.mozilla.org/pipermail/es-discuss/2013-October/034046.html -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131021/a08e9676/attachment.html>
I have a proposal for ES6. I don't know if it's been discussed before, so if it has, I don't mean to waste your time … hopefully, I'll be directed to the right place.
One of the most debated JavaScript topics (at least in my experience) is whether or not one should modify objects that don't belong to them ... some say it's okay if you "know what you're doing" and others see it as a crime.
I propose we solve this problem by adding "refinements" to ES6. Ruby v2.0.0 introduced a nice little feature dubbed "refinements". Refinements allow you to extend other objects for specific modules of code. I know this isn't a Ruby mailing list, but because the inspiration comes from the Ruby implementation, an example is in order (feel free to ignore it).
Say you want to modify the
to_str()
method in the built-in String class in Ruby, the common way to do this is as follows.Very simple ... just re-open the class and create a method with the same name. However, there's a problem with this, which happens to be the same problem we have in JavaScript, and that's the fact that we've now just modified this method permanently for the rest of the execution.
Well, with refinements, the better way of accomplishing the same thing is as follows.
Now, if you try to send
to_str()
to, say, a string object"hello"
("hello".to_str()
), you'll get the original"hello"
as a return value. In order to use the refinements made to the class, you have to do the following.Running the code above properly outputs the string
Blah, blah, blah ... this is an example.
.Refinements can only be used after a
using
statement and only within that file.Given that we're already getting modules in ES6, I believe something similar to this would be a great addition to the language.
I can definitely work on a more in-depth and detailed proposal, with specific JavaScript examples, if needed, but I just would like to hear thoughts around the idea.