Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom toString methods for data classes #33

Closed
passsy opened this issue Feb 13, 2020 · 9 comments
Closed

Custom toString methods for data classes #33

passsy opened this issue Feb 13, 2020 · 9 comments
Labels
enhancement New feature or request

Comments

@passsy
Copy link
Sponsor

passsy commented Feb 13, 2020

I'd love to be able to define custom toString methods for data classes.

@immutable
abstract class ClubId with _$ClubId {
  const factory ClubId(String value) = _ClubIdDataClass;

  @override
  String toString() {
    return 'ClubId#$value';
  }
}

It should skip generation of toString in _$_ClubIdDataClass and rely on the implementation in ClubId.

Should be only possible for @immutable annotated classes with one factory constructor. Alternatively with a custom @freezedDataClass annotation which only allows one factory constructor

@rrousselGit
Copy link
Owner

rrousselGit commented Feb 13, 2020

That's hardly feasible.
The class user-defined is implemented not extended.

The closest we can get is:

@immutable
abstract class ClubId with _$ClubId {
  const ClubId._();
  const factory ClubId(String value) = _ClubIdDataClass;

  @override
  String toString() {
    return 'ClubId#$value';
  }
}

and then Freezed would try to extend this class

@rrousselGit rrousselGit added the enhancement New feature or request label Feb 13, 2020
@zs-dima
Copy link

zs-dima commented Feb 24, 2020

That's hardly feasible.
The class user-defined is implemented not extended.

The closest we can get is:

@immutable
abstract class ClubId with _$ClubId {
  const ClubId._();
***

It looks ugly with additional 'const ClubId._();' line

What if about freezedNoToString attribute for example:

@freezedNoToString
abstract class ClubId with _$ClubId {
  const factory ClubId(String value) = _ClubIdDataClass;

  @override
  String toString() {
    return 'ClubId#$value';
  }
}

@rrousselGit
Copy link
Owner

That's still not feasible.

As I said, the class is implemented not extended. This doesn't allow user defined methods.

@zs-dima
Copy link

zs-dima commented Feb 24, 2020

As I said, the class is implemented not extended. This doesn't allow user defined methods.

And what about use different attribute for the different generation to extend this class as I told about?

toString methods became unreadable with lists and complex objects..

@rrousselGit
Copy link
Owner

I would very much prefer working on the readability instead.

The toString could be made to be multi-line instead

@zs-dima
Copy link

zs-dima commented Feb 24, 2020

The toString could be made to be multi-line instead

How it could be readable with hundred items List<Item> for example?

@rrousselGit
Copy link
Owner

rrousselGit commented Feb 24, 2020

It can be shown last in the toString, or truncated like list: [0, 1,..., 42]

@zs-dima
Copy link

zs-dima commented Feb 27, 2020

list: [0, 1,..., N] is a bit better then print big lists, however it could be big objects where could be nice toString just Id or some short info

@rrousselGit
Copy link
Owner

Closing in favor of #83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants