Skip to content
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

Pre-built Stack Class in the Dart Standard Library #56077

Closed
mdex-geek opened this issue Jun 25, 2024 · 2 comments
Closed

Pre-built Stack Class in the Dart Standard Library #56077

mdex-geek opened this issue Jun 25, 2024 · 2 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-collection type-enhancement A request for a change that isn't a bug

Comments

@mdex-geek
Copy link

Currently, the Dart standard library does not provide a pre-built Stack class like Java. While it's possible to implement a stack using a List, having a dedicated class could offer several benefits:

Improved Readability: Explicit Stack usage clarifies code intent.
Potential Performance Optimizations: A dedicated class might allow for optimizations beyond a basic List implementation.
Type Safety: A built-in stack could offer compile-time type checking for elements.
Why This Feature Would Be Useful:

A pre-built Stack class would enhance the Dart ecosystem by:

Reducing Boilerplate Code: Developers wouldn't need to write their own stack implementations.
Enhancing Consistency: Standardizing stack usage across Dart projects.
Promoting Readability: Making code more explicit and easier to understand.
Alternative Solutions (Acknowledging Current Approach):

We understand that the current approach of using a List for stacks is viable. However, a dedicated class could provide advantages in terms of readability, type safety, and potential performance optimizations.

Open to Discussion:

We're open to discussing the trade-offs between a pre-built Stack class and the existing List approach. We value feedback from the Dart community to determine the best course of action.

Up-vote or Comment:

Please up-vote this issue if you support the addition of a pre-built Stack class to the Dart standard library. Feel free to leave comments with your thoughts and suggestions.

@dart-github-bot
Copy link
Collaborator

Summary: The Dart standard library lacks a dedicated Stack class, which could improve code readability, potential performance, and type safety compared to using a List. The issue proposes adding a pre-built Stack class to the standard library for consistency and reduced boilerplate code.

@dart-github-bot dart-github-bot added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Jun 25, 2024
@lrhn
Copy link
Member

lrhn commented Jun 25, 2024

Currently, the Dart standard library does not provide a pre-built Stack class like Java

You are intended to use List for anything you'd want to use a stack for.
The canonical pattern for declaring a stack is List<Foo> stack = [];.

There is no class called Stack because it's unnecessary when List.add and List.removeLast implements a stack perfectly.

Reducing Boilerplate Code: Developers wouldn't need to write their own stack implementations.

They don't need to do that today. Just use List. Or if you're feeling particularly clever, use Queue, which is a double-ended queue which means it can also be used as a stack.
List has a lower overhead than Queue when used as a stack, and is more efficient on web by using a JavaScript Array directly.

Potential Performance Optimizations: A dedicated class might allow for optimizations beyond a basic List implementation.

That's very unlikely. The API design and implementation of List is based on adding and removing at the end being (amortized) constant time. That followed from JavaScript Array having that behavior, and when compiled to JS, a List is-a JavaScript Array. It doesn't get more optimized than that.

Type Safety: A built-in stack could offer compile-time type checking for elements.

List is typed. I can't see any possible difference in the type safety of the APIs. (Maybe other than not implementing Iterable and therefore not inheriting bool contains(Object?), but you can always choose to not use that on the List.)

However, a dedicated class could provide advantages in terms of readability, type safety, and potential performance optimizations.

Can you give any concrete examples of what those advantages could be?

I can see a modelling argument for using a Stack type which more precisely shows the intent for the data structure than a List.
I can't see any reason a Stack type would have any advantage over List based on type checking or performance.

@lrhn lrhn added library-collection and removed triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Jun 25, 2024
@mdex-geek mdex-geek reopened this Sep 30, 2024
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. library-collection type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants