Giter Site home page Giter Site logo

Comments (8)

tvh avatar tvh commented on August 27, 2024

I like the approach taken by https://pub.dev/packages/sum_types. You could then do the following:

@DataClass
mixin _User {
  String get firstName;
  String get lastName;
  @nullable String get photoUrl;
}

The generated code would then be:

class User with _User {
  const User({
    @required this.firstName,
    @required this.lastName,
    this.photoUrl,
  });
}

When NNBD finally lands one could just replace the @nullable annotations with the new syntax and everything would continue to work.

from data_classes.

MarcelGarus avatar MarcelGarus commented on August 27, 2024

Interesting approach!
But a possible problem I see with this is that it doesn't work well with generators of other packages, for example hive and json_serializable, because mixins are treated as abstract class and thus can't be instantiated by the generated code.

from data_classes.

tvh avatar tvh commented on August 27, 2024

True, that wouldn't be covered.

If only constructors of extended classes would just call the super class with the same args. It could be so easy:

class User extends _UserBase {
  String get firstName;
  String get lastName;
  @nullable String get photoUrl;
}

abstract class _UserBase {
  String firstName;
  String lastName;
  String photoUrl;

  _UserBase({
    this.firstName,
    this.lastName,
    this.photoUrl,
  });
}

This does compile, but completely forgets the parameters.

from data_classes.

skogsbaer avatar skogsbaer commented on August 27, 2024

We implemented the mixin idea of @tvh in https://github.com/factisresearch/sum_data_types
The code in that repo is based on data_classes but the interface is not compatible with data_classes.

from data_classes.

skogsbaer avatar skogsbaer commented on August 27, 2024

Btw: was it intentionally that you use the 4-clause BSD license for data_classes_generator, but BSD 3-clause license for data_classes? It would be easier for us if everything was under BSD 3. Is it possible that you also license the generator part under BSD 3?

from data_classes.

MarcelGarus avatar MarcelGarus commented on August 27, 2024

Oh sorry, that was not intentional. To be honest, I didn't pay too much attention to the license clauses.
Now I changed both to BSD 3.

from data_classes.

MarcelGarus avatar MarcelGarus commented on August 27, 2024

The package could then generate the copy, ==, hashCode and toString methods as static extension methods on the User type.

Oh nooooo, seems like that's not possible. Extensions can't declare members with the same name as a member declared by Object.

from data_classes.

MarcelGarus avatar MarcelGarus commented on August 27, 2024

Okay, I re-evaluated the current ecosystem and it seems like there are really cool alternatives in the data class code generation market. There's, of course, built_value by Google, freezed which has some really cool factory constructor syntax to declare fields and others, like hive are thinking about requiring data classes in the future.
The freezed package, in particular, does many things right and address the same market like this one. All in all, it's more elegant than this one and makes some bold design decisions (like making the copyWith method not type-safe in order to support nullable fields) that make it more intuitive to use.

So, I've been thinking a bit about the future of this package and I believe the best thing to do is to try to get as close to the vision of this issue as possible: support developers writing their own data classes. There's not much tooling in this regard in the existing ecosystem, so I believe this will be a valuable addition to the Dart community rather than another competing framework.

Here's what this package could offer:

  • Implementations for ==, hashCode and toString that developers still have to incorporate into their code manually.
  • A copyWith extension method similar to freezed's one with no type-safety in favor of nullable fields.
  • Extension has getters for all fields.

Using this package would look like this:

@DataClass()
class Fruit {
  User({
    @required this.name,
    @required this.color,
    this.amount = 1,
  });

  final String name;
  final Color color;
  final int amount;

  operator ==(Object other) => _$equals(other);
  int get hashCode => _$hashCode;
  String toString() => _$toString();
}

void main() {
  var apple = Fruit(name: 'Apple', color: Colors.red);
  print(apple.toString());
  var badApple = apple.copyWith(color: Colors.brown);
  badApple.hasNoName; // false
}

from data_classes.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.