Skip to content

List interface contains a last method but not a first method #1790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jacob314 opened this issue Feb 22, 2012 · 3 comments
Closed

List interface contains a last method but not a first method #1790

jacob314 opened this issue Feb 22, 2012 · 3 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug

Comments

@jacob314
Copy link
Member

The Dart List interface contains a last method but not a first method.

I believe it would be consistent with dart if the last and first methods were changed to properties instead of methods as calculating last and first should be extremely cheap for typical implementations.

@alan-knight
Copy link
Contributor

Moving from jjb. lrn, if you'd rather I moved these elsewhere, or didn't move them, let me know.


Set owner to @lrhn.

@lrhn
Copy link
Member

lrhn commented Sep 6, 2012

The "last" method (which I agree should be a getter) is a shorthand for
 list[list.length-1] , which is a (somewhat) long and annoyingly repetitive expression.
When such an expression is used often, it's a good candidate for a shorthand.

For "first", it would always be list[0], which doesn't need a shorthand as much.

Lists are asymmetric - they start from zero, but can have arbitrary lengths. They are designed for random access and growing at the end (a linked list might not be best modelled as a Dart List, but perhaps as a Stack/Queue/DoubleEndedQueue instead). That's why we have "last", "add[Last]" (the "addLast" should be dropped) and "removeLast" as shorthands - we often need to work on that end of the list.

There are generic list[0], list.removeAt(0), list.insertAt(0, e) that works everywhere, and they don't really need a shorthand like acting on the element at position (list.length-1) do. If we had negative-indexing, so -1 was equivalent to (list.length-1), we wouldn't need the 'last' shorthands either (not that there is any plan for that, it's just an example).

That said, if it makes often written code easier to read, it does have merit (I'm in favor of having both isEmpty and hasElements). I'll keep this open as a reminder (and star-counter) for this suggestion.

It's fine assigning to me. I'll try to collect all these suggestions for the "collections overhaul", hopefully somewhere before M2.


Removed Type-Defect label.
Added Type-Enhancement label.

@floitschG
Copy link
Contributor

Fixed in r15040.


Added Fixed label.

@jacob314 jacob314 added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Nov 20, 2012
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants