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

Publicly expose the either! macro? #57

Closed
thomaseizinger opened this issue Aug 16, 2021 · 5 comments · Fixed by #58
Closed

Publicly expose the either! macro? #57

thomaseizinger opened this issue Aug 16, 2021 · 5 comments · Fixed by #58

Comments

@thomaseizinger
Copy link
Contributor

The macro can be useful outside of this crate if one wants to deal with both variants in the same way but without them having the same type. For example:

trait Foo {
    type T;

    fn foo(self) -> Bar<Self::T>;
}

struct Bar<T> {
    inner: T,
}

impl<L, R, LT, RT> Foo for Either<L, R>
where
    L: Foo<T = LT>,
    R: Foo<T = RT>,
{
    type T = Either<LT, RT>;

    fn foo(self) -> Bar<Self::T> {
        let either_bar = either!(self, inner => inner.foo().inner);

        Bar { inner: either_bar }
    }
}
@cuviper
Copy link
Member

cuviper commented Aug 17, 2021

We would need to change it to use $crate, but given that we already export some macros, this seems ok to me.

For public API, we should also consider whether either! is really a good name for what this does. That sounds like something that would construct an Either, but it's really more of a "map" operation -- map_either! perhaps?

@cuviper
Copy link
Member

cuviper commented Aug 17, 2021

I guess it is basically the untyped version of Either::either, so maybe the name either! is ok.
(Whereas Either::map still returns an Either.)

@bluss
Copy link
Contributor

bluss commented Aug 17, 2021

I can't think of a good name off hand. I think we need something different than either - because the macro does not give you two choices- either left or right - it's based around mapping both Left => something and Right => something using just one "expression". Maybe either::both! wouldn't be entirely wrong, but it's awkward as a loose name without its crate.

@thomaseizinger
Copy link
Contributor Author

I guess it is basically the untyped version of Either::either, so maybe the name either! is ok.
(Whereas Either::map still returns an Either.)

Not quite. The main difference is that either! only takes one closure! Whereas the function takes two!

Maybe for_each or for_both would be appropriate?

On the topic of names: Either::either is functionally similar to futures::select!.

@bluss
Copy link
Contributor

bluss commented Sep 18, 2021

Yes, that sounds good to me, with preference to for_both (it's not each - as it's not a container of left and right?)

thomaseizinger added a commit to thomaseizinger/either that referenced this issue Sep 20, 2021
thomaseizinger added a commit to thomaseizinger/either that referenced this issue Sep 20, 2021
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

Successfully merging a pull request may close this issue.

3 participants