dart-lang / dart-lang/language

Add data classes

Open
#314 391 comments 2313 reactions 0 assignees View on GitHub
brevity data-classes request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Immutable data are used heavily for web applications today, commonly with Elm-like (redux, ngrx, ...) architectures. Most common thing web developer is doing with data is creating a copy of it with some fields changed, usually propagated to the root of state tree. JavaScript has spread operator for this. There should be a easy way to use immutable data structures in Dart. I would like to have data classes (inspired by [Kotlin]( https://kotlinlang.org/docs/reference/data-classes.html)) in Dart. Possible API:
```
data class User {
String name;
int age;
}
```
Compiler assumes that all fields of data class are immutable. Compiler adds `equals` implementation based on shallow equals, `hashCode` implementation based on mix of all object fields, `toString` implementation of the form ` =`, and `copy` function to recreate object with some fields changed.

You may argue that there is already [Built value](https://github.com/google/built_value.dart) package that allows to do similar things, but we have many issues with package, mainly:
1. It requires writing a lot of boilerplate code
2. It requires running watcher/manual code generation during development.
3. It requires saving generated files to repository because code generation time is too large for big applications.

I have found that using built value actually decreases my productivity and I do my work faster with even manually writing builders for classes.

If data classes would be implemented on language level, it would increase developer productivity and optimizations can be made when compiling code to particular platform.

Contributor guide

Open the contributing guide

Research direction

No source file, test, or entry point is identified. Start by reading the issue discussion and the linked Kotlin data-classes reference, then compare the proposed API with the concerns about built_value.dart. Done would require an agreed, specified language-level data-class design rather than an isolated implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.