Skip to content

Request to simplify API wrt Right forms #5

Closed

Description

@aztecrex

all right... and ...rights

There is now a proliferation of right... and ...Right methods in
the class. This makes for a really wide interface. I want to consider
a simplification.

Note on #getRight(): that seems OK and should probably stay.

For everything else, what about a #swap() (or maybe a better name) method like so:

// for any...
final Either<L,R> e = ...;
// let...
final Either<R,L> ep = e.swap();

// then...
assert e.isLeft() != ep.isLeft();
assert e.isLeft() || e.getRight().equals(ep.getLeft());
assert !e.isLeft() || e.getLeft().equals(ep.getRight());

So, rather than, e.g.:

e.rightOrElse("other");
e.maybeRight();

You would use:

e.swap().orElse("other");
e.swap().maybe();

Here is something interesting:

Either<Integer, String> e = 
  Either.of(Something::thatMightThrow)
     .swap().map(RuntimeException::getMessage)
     .swap()
     .map(My::method)
     ...
     ...
     ; 

Without #swap(), Either would need a #rightMap(..) or similar to
achieve the above.

I am not fully this is a better interface model. Would like to
see comments from others before making a decision.

Activity

  1. aztecrex commented on Sep 17, 2014

    @aztecrex
    MemberAuthor

    This has been implemented and release in 0.10.

    Still not sure 'swap' is the right method name. Hoping to think of a better one before 1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions