-
Notifications
You must be signed in to change notification settings - Fork 13
naming about end
#11
Comments
I’d be very surprised if a non-negligible number of users even knew about the spec names for those arguments, or thought about it in those terms. Regardless, though, “padEnd”, “endsWith”, and “trimEnd” are far more prominent examples of “end means the end” - that’s what I’d expect people would think of. |
@mysticatea if you have a better name I'm certainly all ears. |
I'm sorry, I have no idea. Maybe @ljharb I think I'm pulled by C++ knowledge 😄 |
I think that |
The British citizen in me likes the idea of
In all seriousness. We can bikeshed on the name, probably ad infinitum.
|
I like Yes, it’s a compound word, but when I’m thinking about a coding newbie, I don’t think it would be obvious to them at all whether Being explicit never really hurt anybody, and I really feel that |
I reacted to @addaleax because I also feel that "end" is possibly a bit vague whereas |
Agree with @addaleax, there could be provided both options const myFancyArray = ['dog', 'cat', 'bird'];
console.log(myFancyArray.lastItem); // 'bird'
console.log(myFancyArray.lastIndex); // 2
console.log(myFancyArray.length); // 3 in this proposal. |
I would like to see const myArray = [ 1, 2, 3 ]
assert( myArray.last === 3 ) // like the return from pop()
assert( myArray.head === 1 ) // like the return from shift()
assert( myArray.tail === [ 2, 3 ] ) // like the mutated result of shift() |
@coryasilva If you're suggesting including const arr = [1, 2, 3];
assert(arr.init === [1, 2]); // Same as arr.slice(0, -1);
assert(arr.tail === [2, 3]); // Same as arr.slice(1); |
Random bikeshed idea, probably not really worth it. But, how about
;) In other words you'd be able to write this: const arr = [1, 2, 3];
assert( arr[Infinity] === 3 ); // As if you were trying to use the index `Infinity` // This actually works today
const arr = [1, 2, 3];
arr.slice(1, Infinity); // => [2, 3] // And Infinity even works with type checking
const arr:Array<number> = [1,2,3];
function incrementIndex(i:number):void {
arr[i] += 1;
}
incrementIndex(0);
// incrementIndex('end'); // Type error and "string isn't an array index" without the explicit `:number` type
incrementIndex(Infinity); // Infinity is a number, and flow also doesn't say Infinity isn't an array index |
You can already have any own string property on any array (part of the basic nature of JS, which i don’t think type systems accommodate), so that wouldn’t work: var a = [];
a.Infinity = true;
a[Infinity] // true |
@ljharb I'm aware of that. But how is that any different from 'end'? var a = [];
a.end = true;
a['end'] // true We're just bikeshedding over a name for that property, and neither 'end' or 'Infinity' are valid array indexes or valid values for |
Ah, i misunderstood your suggestion - you’re saying we should name the getter “Infinity”? I see how that would work, but i think the likelihood of someone accidentally putting an Infinity in where they intended a specific index is a problem - currently they’d get undefined, but with your change they might not notice the bug. |
I guarantee calling it C++ itself uses |
Switching languages back and forth isn’t a use case we prioritize (altho consistency with other languages is nice, when achievable and when there’s a broad existing convention). JS API needs to work well for JS. |
I would suggest maintaining something similar to the age old, tried and true Likewise, a companion method for The prefix |
If you wanted to go with stack-like semantics (Arrays are kind of a hybrid List/Stack/Queue), you'd use "peek" for the last item; but that wouldn't provide a way to set it, and arrays are more a List than a Stack or a Queue anyways. |
@gmattie please see https://github.com/keithamus/proposal-array-last#other-considered-options for rationale on why I will be presenting this in the March meeting, and will bring up the |
Who am I to argue with Twitter polls? https://twitter.com/Keithamus/status/973889308158578689
|
I would prefer end. :( Twitter polls are not particularly official at least with github when can see the voters. |
No doubt that |
I like |
end
is well-known as the location of the next of the last element. For example,end
is used as the end (exclusive) of range in Array.prototype.slice(start, end) and Array.prototype.fill(value, start, end).So I think that
anArray.end
may cause confusion. Especially,anArray.end = 1
looks like the assignment to the next of the last element.The text was updated successfully, but these errors were encountered: