Git Product home page Git Product logo

Comments (1)

lrhn avatar lrhn commented on May 5, 2024

Just for posterity: I Cannot answer for the original authors, but most likely it's because the desired equality is actually element equality.
The typles are used as simple value types, and it would probably be surprising if you did:

var pair = Tuple2(1, 2);
Set<Tuple2<num, num>> set = {pair};
print(set.contains(Tuple2(1, 2)));

and it printed false. Which it would, becasue var pair = Tuple2(1, 2); has no context type, so it infers Tuple2<int, int>, which contains(Tuple2(1, 2)) has a context type of Tuple<num, num> and would therefore create an instance of that.

Tuples are immutable, so the optimal runtime type would be type arguments that were the runtime type of the elements. A Tuple2<num, num>(1, 2) is strictly less useful than a Tuple2<int, int>(1, 2), because the latter can be assigned to Tuple2<num, num> and then works fine at that type, but the former cannot be used as a Tuple2<int, int> at all.
(And that's what Dart 3.0's records do: Their runtime type is the record type of their fields' objects runtime type.)

So the class will allow you to give every tuple its optimal type arguments, but not punish you if it fails (not until you try a cast which won't work).
Using only the values for equality is allowing precisely that

Secondarily, if the type parameters were part of equality, they should probably also be part of hashCode, which means taking the hash code of reified Type objects.
It'd not be wrong to not include the types in the hash code, after all it's not incorrect for non-equal values to have the same hash code, but it would risk hash code conflicts, if a Tuple2<int, int>(1, 2) and Tuple2<num, num>(1, 2) ended up in the same hash set/map.

from tuple.dart.

Related Issues (16)

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.