Skip to content
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

[Question] Either union like from Dartz #27

Closed
petrnymsa opened this issue Feb 12, 2020 · 2 comments
Closed

[Question] Either union like from Dartz #27

petrnymsa opened this issue Feb 12, 2020 · 2 comments

Comments

@petrnymsa
Copy link

In Dartz package we have Either<L,R> type to represent either L type or R type.
I thought that this should mimic this behavior

@immutable
abstract class Either<L, R> with _$Either<L, R> {
  const factory Either.left(L left) = Left;
  const factory Either.right(R right) = Right;
}

and usage

void run(Either<int, String> either) {
  either.when(left: (value) => print(value), right: (value) => value);
}
---
run(Left(10));

However, I am getting the following error

lib/either.dart:7:39: Error: The constructor function type 'Left<dynamic, dynamic> Function(dynamic)' isn't a subtype of 'Either<L, R> Function(L)'.
 - 'Left' is from 'package:sandbox/either.dart' ('lib/either.dart').
 - 'Either' is from 'package:sandbox/either.dart' ('lib/either.dart').
  const factory Either.left(L left) = Left;
                                      ^
lib/either.dart:8:41: Error: The constructor function type 'Right<dynamic, dynamic> Function(dynamic)' isn't a subtype of 'Either<L, R> Function(R)'.
 - 'Right' is from 'package:sandbox/either.dart' ('lib/either.dart').
 - 'Either' is from 'package:sandbox/either.dart' ('lib/either.dart').
  const factory Either.right(R right) = Right;
                                        ^

Am I doing something wrong? Or should it be possible to create this? Thanks for any help.

@rrousselGit
Copy link
Owner

The syntax is:

@immutable
abstract class Either<L, R> with _$Either<L, R> {
  const factory Either.left(L left) = Left<L, R>;
  const factory Either.right(R right) = Right<L, R>;
}

@petrnymsa
Copy link
Author

@rrousselGit Thank you, really stupid mistake :)

We can close this issue. And thank you for really great package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants