> 1. Considering that, in the example, adding the type to the hash would make the hash codes to be different, does this imply in some kind of potential performance improvement, as it will avoid `==` being called?
I can only see this benefit if you have a Map/Set with entities of
mixed types of classes which ends up having the same internal value
but still needs to be seen as different entities. I think personally
this is such a rare case that it is not worth adding the type to the
hashsum and I have yet to see anybody adding the type to the hashsum.
From a performance perspective, it should not really matter but it
does make it more "expensive" to calculate the hashSum since you
include yet another parameter which are very likely to never matter.
But that is such a minor thing that I would say it can be ignored
(unless somebody comes and say the hashCode of a Type is a very
expensive operation) :)
> 2. Is there any downside in including the type in the hash? I think the fact that it make the two hash codes different is either positive or neutral, so what would be the reason to not include it?
I guess one downside could be that it makes it more complicated to
make subclass that should behave the same as the original object but
it depends on how you would include the class in the hashsum. E.g. are
you going to include .runtimeType it would make it rather annoying but
if you just manually have used "AType.hashsum" then I guess it is less
problematic.
But I don't see any benefit really of doing this and you would just
make your code more complicated without any gains. So my own personal
recommendation would be to NOT include the type of your class as part
of hashcode generation.
Den tirs. 14. nov. 2023 kl. 11.27 skrev Mateus Felipe Cordeiro Caetano
Pinto <
mateu...@gmail.com>:
>
> I am sorry for my initial question, I was writing it and accidentally posted it before finishing, but I think the overall idea has been passed.
>
> > But again, if the hashCode's ends up being equal, you need to afterwards call == to be sure.
>
> I think my question is around this. I know that `==` is the "canonical" source of truth regarding equality. However, it's not clear how hash codes are supposed to be used, and even less how they ARE used internally by Dart.
>
> Sure, the documentation says that it's used for hash based data structures like the default Set and Map implementations, but there's no details about how this works. To be more specific, I'm going to split my question in two:
>
> 1. Considering that, in the example, adding the type to the hash would make the hash codes to be different, does this imply in some kind of potential performance improvement, as it will avoid `==` being called?
> 2. Is there any downside in including the type in the hash? I think the fact that it make the two hash codes different is either positive or neutral, so what would be the reason to not include it?
>
> Thanks!