+erights (who suggested a way to bind compare to Collator by default in the
ECMAScript discussion list) and arv

Hi Mads,

Thank you for the reply.

I'm torn between two alternatives. Initially, I was inclined to go with what
we have now. That is, clearly document what to do when passing compare()
directly to sort() (either A or B,, below where |coll| is a Collator.) and
live with complaints of those who don't RTFM :-).

A.
array.sort(function (a,b) { return coll.compare(a,b); })

B.
array.sort(coll.compare.bind(coll))

In the ecmascript mailing list,  when Axel Hecht asked if we could make it
bound to a Collator to make the life of "casual" users easier, Mark
suggested a way to do so.  Making the life of 'casual' users easy is a bit
tempting (although not very much).

Now the question boils down to :

1) which is more important,
    a) to make the life of casual ('clueless'?) API users simple (who don't
RTFM)
    b) to preserve what JavaScript always does
          (a function is extracted and get bounds to a global object unless
explicitly bound).

2) if we go with a) above, what else would we lose other than 'deviating'
from the way 'JavaScript' works?

If |compare| is bound by default to a Collator, can it be still 'rebound' to
something else if necessary? Well, I can't think of any use case for that,
but there may be something I don't know about.

If there's nothing to lose by doing 1a) above (the answer to Q2 is nothing),
I'm now tilted toward doing it.

What would you say to the above two questions?

Thank you,


Jungshik








On Tue, Mar 22, 2011 at 11:28 AM, Mads Sig Ager <[email protected]> wrote:

> I would prefer not to do this. I would leave it to the developer to make
> sure that the functions that he passes to sort are correctly bound to the
> right things. That is the way JavaScript works and I don't think we should
> attempt to hide that fact here?
>
> Cheers,    -- Mads
>
>
> On Tue, Mar 22, 2011 at 5:30 PM, <[email protected]> wrote:
>
>> The question still stands, should we actually do this (.bind) or not,
>> since most
>> of the core API don't...
>>
>>
>> On 2011/03/22 16:04:17, Nebojša Ćirić wrote:
>>
>>> Also, defineProperty is part of Harmony but it seems only Opera doesn't
>>> have
>>>
>> it
>>
>>> (FF4, IE9, Chrome 5 have it, and IE8 only for DOM).
>>>
>>
>>  On 2011/03/22 16:02:58, Nebojša Ćirić wrote:
>>> > I've presented the problem to EcmaScript body, and they proposed 2
>>>
>> solutions.
>>
>>> >
>>> > Q:
>>> >
>>> > So if we have something like this:
>>> >
>>> > LocaleInfo.Collator.prototype.compare = function(a, b) {
>>> > ...
>>> > };
>>> >
>>> > How do you specify it's bound by default?
>>> >
>>> > A:
>>> >
>>> > Two choices:
>>> >
>>> > 1) Don't put it on the prototype. Rather, make it part of each instance
>>>
>> state,
>>
>>> > don't use "this" in the function, and have the function lexically
>>> capture
>>>
>> all
>>
>>> > the state it needs.
>>> >
>>> > 2) Put it on the prototype using a binding accessor:
>>> >
>>> > function makeBindingAccessorDescriptor(method) {
>>> >     return {
>>> >         get: function() { return method.bind(this); }
>>> >         set: writable: ??, enumerable: ??, configurable: ?? //
>>> according to
>>> your
>>> > intensions
>>> >     };
>>> > }
>>> >
>>> > Object.defineProperty(LocaleInfo.Collator.prototype, 'compare',
>>> >
>>>  makeBindingAccessorDescriptor(function(a,
>>>
>> b)
>>
>>> {
>>> >     ....
>>> > }));
>>> >
>>> > More details about definePrototype and get/set statements -
>>> > http://ejohn.org/blog/ecmascript-5-objects-and-properties/
>>> >
>>> > Now the question is, can we do 2) in v8...
>>> >
>>> > On 2011/03/15 07:55:48, Mads Ager wrote:
>>> > > On 2011/03/14 23:23:01, Jungshik Shin wrote:
>>> > > > Thank you for the explanation.
>>> > > >
>>> > > > Is there any way to make 'array.sort(coll.compare)' work on my side
>>> (in
>>> C++)
>>> > > or
>>> > > > is that the way sort() is supposed to work no matter what? It seems
>>> that
>>> the
>>> > > > latter is the case, but I'd better ask you to make sure.
>>> > >
>>> > > This is just the way JavaScript works. You are extracting the
>>> function and
>>> > > calling it with a receiver that is not a collator. You are right that
>>> you
>>> > cannot
>>> > > make this work this way. I don't think you should in any case because
>>> this
>>> is
>>> > > not how JS works.
>>> > >
>>> > > > I guess many callers want to define their own compare function any
>>> way
>>> > because
>>> > > > they're likely to compare more complex objects than simple strings.
>>> For
>>> > > > instance, a file browser may want to list directories before
>>> regular
>>>
>> files
>>
>>> > and
>>> > > > would use the following:
>>> > > >
>>> > > > var compareFileName = function(a, b) {
>>> > > >    if (a.type == b.type) { return coll.compare(a.name, b.name);}
>>> > > >    if (a.type =="directory") return -1;
>>> > > >    return 1;
>>> > > > }
>>> > > >
>>> > > > So, not being able to directly pass coll.compare() is probably ok.
>>> However,
>>> > if
>>> > > > there's any way to make it possible, pls let me know.
>>>
>>
>>
>>
>> http://codereview.chromium.org/6673011/
>>
>
>

-- 
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to