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) 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 <Type> <fieldN>=<valueN>
, and copy
function to recreate object with some fields changed.
You may argue that there is already Built value package that allows to do similar things, but we have many issues with package, mainly:
- It requires writing a lot of boilerplate code
- It requires running watcher/manual code generation during development.
- 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.