Skip to content

Commit

Permalink
Cleanup typing of Tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Jun 24, 2023
1 parent 1327e5d commit d0fc32b
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 68 deletions.
12 changes: 6 additions & 6 deletions bin/generate_tuple.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,17 @@ Future<void> generateImplementation(int i) async {
// iterable
out.writeln();
out.writeln('/// An (untyped) [Iterable] over the values of this tuple.');
out.writeln('Iterable<Object?> get iterable sync* {');
out.writeln('Iterable<dynamic> get iterable sync* {');
for (var j = 0; j < i; j++) {
out.writeln('yield ${values[j]};');
}
out.writeln('}');
out.writeln();
out.writeln('/// An (untyped) [List] with the values of this tuple.');
out.writeln('List<Object?> toList() => [${listify(values)}];');
out.writeln('List<dynamic> toList() => [${listify(values)}];');
out.writeln();
out.writeln('/// An (untyped) [Set] with the unique values of this tuple.');
out.writeln('Set<Object?> toSet() => {${listify(values)}};');
out.writeln('Set<dynamic> toSet() => {${listify(values)}};');

out.writeln('}');
await out.close();
Expand Down Expand Up @@ -368,13 +368,13 @@ Future<void> generateTest() async {
out.writeln('), $result);');
});
nest('test', 'iterable', () {
out.writeln('expect(tuple.iterable, <Object>[${listify(numbers)}]);');
out.writeln('expect(tuple.iterable, <dynamic>[${listify(numbers)}]);');
});
nest('test', 'toList', () {
out.writeln('expect(tuple.toList(), <Object>[${listify(numbers)}]);');
out.writeln('expect(tuple.toList(), <dynamic>[${listify(numbers)}]);');
});
nest('test', 'toSet', () {
out.writeln('expect(tuple.toSet(), <Object>{${listify(numbers)}});');
out.writeln('expect(tuple.toSet(), <dynamic>{${listify(numbers)}});');
});
});
}
Expand Down
9 changes: 9 additions & 0 deletions lib/src/functional/either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ sealed class Either<L, R> {
/// Returns the right value as an [Optional].
Optional<R> get rightOptional;

/// Returns a tuple with either the left or right value defined.
(L?, R?) get tuple;

/// Returns `true`, if this is a left value.
bool get isLeft;

Expand Down Expand Up @@ -138,6 +141,9 @@ class LeftEither<L, R> extends Either<L, R> {
@override
Optional<R> get rightOptional => Optional<R>.absent();

@override
(L, R?) get tuple => (leftValue, null);

@override
bool get isLeft => true;

Expand Down Expand Up @@ -207,6 +213,9 @@ class RightEither<L, R> extends Either<L, R> {
@override
Optional<R> get rightOptional => Optional<R>.of(rightValue);

@override
(L?, R) get tuple => (null, rightValue);

@override
bool get isLeft => false;

Expand Down
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ extension Tuple0 on () {
R map<R>(R Function() callback) => callback();

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {}
Iterable<dynamic> get iterable sync* {}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [];
List<dynamic> toList() => [];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {};
Set<dynamic> toSet() => {};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ extension Tuple1<T1> on (T1,) {
R map<R>(R Function(T1 first) callback) => callback($1);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1];
List<dynamic> toList() => [$1];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1};
Set<dynamic> toSet() => {$1};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ extension Tuple2<T1, T2> on (T1, T2) {
R map<R>(R Function(T1 first, T2 second) callback) => callback($1, $2);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2];
List<dynamic> toList() => [$1, $2];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2};
Set<dynamic> toSet() => {$1, $2};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ extension Tuple3<T1, T2, T3> on (T1, T2, T3) {
callback($1, $2, $3);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
yield $3;
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2, $3];
List<dynamic> toList() => [$1, $2, $3];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2, $3};
Set<dynamic> toSet() => {$1, $2, $3};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ extension Tuple4<T1, T2, T3, T4> on (T1, T2, T3, T4) {
callback($1, $2, $3, $4);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
yield $3;
yield $4;
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2, $3, $4];
List<dynamic> toList() => [$1, $2, $3, $4];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2, $3, $4};
Set<dynamic> toSet() => {$1, $2, $3, $4};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extension Tuple5<T1, T2, T3, T4, T5> on (T1, T2, T3, T4, T5) {
callback($1, $2, $3, $4, $5);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
yield $3;
Expand All @@ -103,8 +103,8 @@ extension Tuple5<T1, T2, T3, T4, T5> on (T1, T2, T3, T4, T5) {
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2, $3, $4, $5];
List<dynamic> toList() => [$1, $2, $3, $4, $5];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2, $3, $4, $5};
Set<dynamic> toSet() => {$1, $2, $3, $4, $5};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_6.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ extension Tuple6<T1, T2, T3, T4, T5, T6> on (T1, T2, T3, T4, T5, T6) {
callback($1, $2, $3, $4, $5, $6);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
yield $3;
Expand All @@ -125,8 +125,8 @@ extension Tuple6<T1, T2, T3, T4, T5, T6> on (T1, T2, T3, T4, T5, T6) {
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2, $3, $4, $5, $6];
List<dynamic> toList() => [$1, $2, $3, $4, $5, $6];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2, $3, $4, $5, $6};
Set<dynamic> toSet() => {$1, $2, $3, $4, $5, $6};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_7.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ extension Tuple7<T1, T2, T3, T4, T5, T6, T7> on (T1, T2, T3, T4, T5, T6, T7) {
callback($1, $2, $3, $4, $5, $6, $7);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
yield $3;
Expand All @@ -147,8 +147,8 @@ extension Tuple7<T1, T2, T3, T4, T5, T6, T7> on (T1, T2, T3, T4, T5, T6, T7) {
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2, $3, $4, $5, $6, $7];
List<dynamic> toList() => [$1, $2, $3, $4, $5, $6, $7];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2, $3, $4, $5, $6, $7};
Set<dynamic> toSet() => {$1, $2, $3, $4, $5, $6, $7};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ extension Tuple8<T1, T2, T3, T4, T5, T6, T7, T8> on (
callback($1, $2, $3, $4, $5, $6, $7, $8);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
yield $3;
Expand All @@ -180,8 +180,8 @@ extension Tuple8<T1, T2, T3, T4, T5, T6, T7, T8> on (
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2, $3, $4, $5, $6, $7, $8];
List<dynamic> toList() => [$1, $2, $3, $4, $5, $6, $7, $8];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2, $3, $4, $5, $6, $7, $8};
Set<dynamic> toSet() => {$1, $2, $3, $4, $5, $6, $7, $8};
}
6 changes: 3 additions & 3 deletions lib/src/tuple/tuple_9.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ extension Tuple9<T1, T2, T3, T4, T5, T6, T7, T8, T9> on (
callback($1, $2, $3, $4, $5, $6, $7, $8, $9);

/// An (untyped) [Iterable] over the values of this tuple.
Iterable<Object?> get iterable sync* {
Iterable<dynamic> get iterable sync* {
yield $1;
yield $2;
yield $3;
Expand All @@ -163,8 +163,8 @@ extension Tuple9<T1, T2, T3, T4, T5, T6, T7, T8, T9> on (
}

/// An (untyped) [List] with the values of this tuple.
List<Object?> toList() => [$1, $2, $3, $4, $5, $6, $7, $8, $9];
List<dynamic> toList() => [$1, $2, $3, $4, $5, $6, $7, $8, $9];

/// An (untyped) [Set] with the unique values of this tuple.
Set<Object?> toSet() => {$1, $2, $3, $4, $5, $6, $7, $8, $9};
Set<dynamic> toSet() => {$1, $2, $3, $4, $5, $6, $7, $8, $9};
}
6 changes: 6 additions & 0 deletions test/functional_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ void main() {
test('rightOptional', () {
expect(either.rightOptional.isAbsent, isTrue);
});
test('tuple', () {
expect(either.tuple, (value, null));
});
test('isLeft', () {
expect(either.isLeft, isTrue);
});
Expand Down Expand Up @@ -330,6 +333,9 @@ void main() {
test('rightOptional', () {
expect(either.rightOptional.orElseThrow(), value);
});
test('tuple', () {
expect(either.tuple, (null, value));
});
test('isLeft', () {
expect(either.isLeft, isFalse);
});
Expand Down
Loading

0 comments on commit d0fc32b

Please sign in to comment.