tuple.dart

repository·master·Indexed 41 days ago

https://github.com/google/tuple.dart

A Dart library providing immutable tuple data structures (Tuple2 through Tuple7) for bundling multiple objects of different types into a single structure. Features include value-based equality, conversion to lists via toList(), and the ability to create modified copies using withItemN methods. Note: For projects using Dart 3.0 or later, native Records are recommended over this package.

Tokens
980
Snippets
10
Records
10
Agent score
2%

What's inside tuple.dart

  1. Consider using Dart Records instead of package:tuple

    master

    Since Dart 3.0, the language includes native Records, which are anonymous, immutable, aggregate types that serve the same purpose as package:tuple.

    New projects should prefer native Dart Records over this package. This package is considered feature-complete and will only receive bug fixes, not new functionality enhancements.

    var record = (123, true);
    print('${record.$1}: ${record.$2}');
  2. Use Tuple classes to bundle multiple objects

    master

    The tuple.dart library provides a set of immutable tuple classes (Tuple2 through Tuple7) used to bundle multiple objects of different types into a single structure. Each tuple class is generic, allowing you to specify the types of its elements.

    Key Features:

    • Immutability: Tuples are immutable. To 'change' a value, use the withItemN methods which return a new tuple instance.
    • Equality: Tuples implement operator == and hashCode based on their contents, meaning two different tuple instances are considered equal if all their corresponding items are equal.
    • Conversion: You can convert any tuple to a List using the toList() method.
    • Creation: Tuples can be created via their standard constructor or by using fromList(List items).
    const t = const Tuple2<String, int>('a', 10);
    
    print(t.item1); // prints 'a'
    print(t.item2); // prints '10'
    
    // Creating a new tuple with a modified value
    final t2 = t.withItem1('c');
    // t2 is a new Tuple2 object with item1 as 'c' and item2 as 10.
  3. Create a new Tuple instance with modified values

    master

    Since tuples are immutable, you cannot modify their items directly. To create a new tuple with one value changed, use the .withItemN() method (e.g., .withItem1() or .withItem2()). This returns a new instance containing the updated value and the original values for all other items.

    const t1 = Tuple2<String, int>('a', 10);
    final t2 = t1.withItem1('c');
    // t2 is a new [Tuple2] object with item1 is 'c' and item2 is 10.
  4. Use Tuple2 for immutable data bundling

    master

    The tuple package provides immutable tuple data structures. You can create a Tuple2 object by specifying the types for its two items and providing the values. Access individual items using the .item1 and .item2 properties.

    const t = Tuple2<String, int>('a', 10);
    
    print(t.item1); // prints 'a'
    print(t.item2); // prints '10'