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
Allow custom toString, hashCode, equals #93
Comments
They're also only good for debugging, not for more practical uses. +1 |
Makes sense, and this should apply for equals and hashCode too. For your specific case though, I wonder if it would be better to improve the generated toString. Another option would be to post-process the output of toString when you want to log it. If lots of people end up writing their own toString then I'd say the generated code has issues :) What do you think? |
Not sure about this, I've never needed this for built values. But if it's easier to do it all at once, sure.
FWIW, I think I mostly use toString (of any class, not just builtvalues) for logging. For that, I do things like
I then use it like this:
It needs to be one-line, short, and immediately recognizable for me as a programmer. I don't think this can be automated (unless you want to introduce some kind of annotation like The |
#153 doesn't quite do this -- but maybe it does exactly what you want :) ... by allowing easily customizable toString. Built-in options are now multiline with indentation or single line. |
Interesting. Does this feature have documentation somewhere? I'm not sure where to customize. |
The PR is not merged yet :) ... customization will be here, you can inject your own BuiltValueToStringHelper: 82d0fb9#diff-83380c0b45d211065789ba69356b1effR104 Example: |
I was just investigating the dart2js output in https://dart-lang.github.io/dump-info-visualizer Somewhat related to #155 (dart2js optimization) |
Thanks! Actually I think my pending PR will help there too, because as you can see the literals now match: |
Yes, that should work as well. |
Could we allow for providing custom @override
bool operator ==(Object other) {
if (identical(other, this)) return true;
return other is SomeItem && id == other.id;
}
@override
int get hashCode => id.hashCode; for example in kotlin data classes you can override them freely, and in lombok (code gen for java) you can specify which fields to use when generating equals and hashcode:
|
You can do that using the https://pub.dev/documentation/built_value/latest/built_value/BuiltValue/defaultCompare.html then set it for the id field to true: https://pub.dev/documentation/built_value/latest/built_value/BuiltValueField/compare.html |
I have no idea how I've missed that... Thanks for the reply, you're the best! ❤ |
I'd like to use DeepCollectionEquality to compare one of the fields on my Built class (and to compute its hashCode). Is there any way to customize this given that we can't override operator== and hashCode? |
You should usually use built_collection classes with built_value, those do deep comparisons and hash codes already. They also cache hash values to optimize. |
So at the top level I have a built value class, which has a class DeepCollectionEqualityKey {
DeepCollectionEqualityKey(this.key);
Object key;
@override
bool operator ==(Object other) =>
other is DeepCollectionEqualityKey &&
DeepCollectionEquality().equals(key, other.key);
@override
int get hashCode => DeepCollectionEquality().hash(key);
} and I changed the map to So I tried to make DeepCollectionEqualityKey a built value, but built doesn't allow you to override == and hashCode. Looking at the My thinking is that I probably have to create a custom serializer instead, but just wanted to check if there's a better way to do it with built_value |
I'm not sure I follow ... if the keys are out of your control, how is it that they are already serializable? built_value should serialize to built_collection types, which would make the compares work as desired. |
Yeah I think it's a fair question (the short answer is that we don't really have those guarantees aside from documentation and testing infrastructure). I guess we rely heavily enough on Built for serialization, that we should probably just rely on it for equality as well. Thanks for talking through it. |
It looks like
toString()
is overridden by default. I'd like to have the option to provide my owntoString()
, and have built_value provide it only when it's not explicitly defined on the abstract class.The strings generated by built_value contain a lot of newlines and get messy, especially when logging (the number one reason to call toString for me).
The text was updated successfully, but these errors were encountered: