On 17 August 2012 20:54, Brendan Eich <[email protected]> wrote:
> Andreas Rossberg wrote:
>>
>> On 16 August 2012 23:47, Brendan Eich<[email protected]> wrote:
>>> Andreas Rossberg wrote:
>>> -1 as an out-of-band (for non-negative indexes) return code is actually
>>> easier to test
>>
>> Why is it easier to compare against constants -1, NaN, or "" than to,
>> say, null or undefined? And if so, wouldn't that be something that
>> might be worth correcting? Question mark operator, anyone?
>>
>> let index = s.find(...)
>> if (?index) { // equivalent to index !== undefined
>> ...
>> } else {
>> // err...
>> }
>
> We've toyed with that operator, but my point was that you don't need an if
> of that kind of you can write a combined index test, e.g. in a cheezy
> split-like example:
>
> js> var s = "a b c "
> js> var a = []
> js> var i, j = -1
> js> while ((i = s.indexOf(' ', j+1)) >= 0) { a.push(s.slice(j+1, i)); j = i; }
> 5
> js> a
> ["a", "b", "c"]
>
> In this case, undefined as indexOf sentinel value would work too because it
> converts to NaN -- but null would not work (it converts to 0). Using an
> "in-band OOB " (for number type) sentinel wins.
Hm, I don't see how this example relies on an in-band sentinel. The
loop condition would work just as well with a comparison to undefined.
Everything else is regular argument values.
FWIW, the example is clearer when written as
var i, j = 0
while ((i = s.indexOf(' ', j)) >= 0) { a.push(s.slice(j, i)); j = i+1; }
But that's equivalent, and your use of "off-by-one" initialization not
actually related to sentinels.
/Andreas
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss