IssueRaiser

Add data classes

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 `<Type> <fieldN>=<valueN>`, 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

Posted by ranquild on Oct 31, 2017, 11:42 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

github (Software)

Add data classes

ranquildOct 31, 2017, 11:42 AM

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 `<Type> <fieldN>=<valueN>`, 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

Comments (0)

No comments yet. Start the discussion.

Comment on this issue