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

Map should be applied only if payload is present #28

Open
stonedMoose opened this issue Aug 30, 2021 · 0 comments
Open

Map should be applied only if payload is present #28

stonedMoose opened this issue Aug 30, 2021 · 0 comments

Comments

@stonedMoose
Copy link

stonedMoose commented Aug 30, 2021

I think map should be applied only if payload is present.

It gives us the ability to chain map method whether the paylaod is present or not.

If you look at the java implementation, it is implemented that way.

    public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
        Objects.requireNonNull(mapper);
        if (!isPresent()) {
            return empty();
        } else {
            return Optional.ofNullable(mapper.apply(value));
        }
    }

The current implementation force the developer to null-check value in the mapper. It seems kind of counter intuitive to me.

// currently I do this
  this.phone = Optional.ofNullable(builder.phone).map((phone) => {
    if (phone) {
      Assert.isValidPhoneNumber(builder.phone);
      return new Name(phone);
    }
    return undefined;
  });

// while it could simply be 
  this.phone = Optional.ofNullable(builder.phone).map((phone) => {
      Assert.isValidPhoneNumber(builder.phone);
      return new Name(phone);
    }  });

I could suggest a PR if you're willing to accept this change.

@bromne bromne added this to To do in typescript-optional via automation Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

1 participant