dart-lang / sdk

The Dart SDK, including the VM, dart2js, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
9.93k stars 1.53k forks source link

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

Open mdex-geek opened 3 days ago

mdex-geek commented 3 days ago

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 commented 3 days ago

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.

lrhn commented 3 days ago

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.