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

let's use Expect in all atoms #3461

Open
yegor256 opened this issue Nov 4, 2024 · 5 comments
Open

let's use Expect in all atoms #3461

yegor256 opened this issue Nov 4, 2024 · 5 comments

Comments

@yegor256
Copy link
Member

yegor256 commented Nov 4, 2024

Currently, we use Expect class just in a few atoms, where we want to have a detailed error reporting in case of user errors. Let's use it in all atoms.

@asmirnov-backend
Copy link
Contributor

@yegor256 Should Expect be used for checking Attr.RHO?
For examlpe, transform this

new Dataized(this.take(Attr.RHO)).asNumber()

to this

Expect.at(this, Attr.RHO)
      .that(phi -> new Dataized(phi).asNumber())
      .otherwise("Attr.RHO must be a number for 'EOnumber'")
      .it();

@yegor256
Copy link
Member Author

@asmirnov-backend yes, definitely, this will be helpful

asmirnov-backend added a commit to asmirnov-backend/eo that referenced this issue Jan 16, 2025
@asmirnov-backend
Copy link
Contributor

While using Expect some code duplicates appear, for example, next piece of code used in EOnumber$EOdiv, EOnumber$EOgt, EOnumber$EOplus, EOnumber$EOtimes (Ref: #3789)

Expect.at(this, Attr.RHO)
    .that(phi -> new Dataized(phi).asNumber())
    .otherwise("must be a number")
    .it();

Perhaps it would be useful to have a more clever Dataized with Expect class.

public class DataizableExpect {

    private final Expect<Phi> expect;

    public DataizableExpect(final Expect<Phi> expect) {
        this.expect = expect;
    }

    public Double asDouble() {
        return this.expect
            .that(phi -> new Dataized(phi).asNumber())
            .otherwise("must be a number")
            .it();
    }

    public Integer asPositiveInt() {
        return this.expect
            .that(phi -> new Dataized(phi).asNumber())
            .otherwise("must be a number")
            .must(number -> number % 1 == 0)
            .that(Double::intValue)
            .otherwise("must be an integer")
            .must(integer -> integer >= 0)
            .otherwise("must be a positive integer")
            .it();
    }

}

The code that uses this class would look something like this:

new DataizableExpect(Expect.at(this, Attr.RHO)).asDouble()

@yegor256 What do you think?

@yegor256
Copy link
Member Author

@asmirnov-backend maybe we can make inner classes:

new Expect.Number(Expect.at(this, Attr.RHO)).it();
new Expect.Integer(Expect.at(this, Attr.RHO)).it();
new Expect.Whole(Expect.at(this, Attr.RHO)).it(); // greater or equal to zero

WDYT?

@asmirnov-backend
Copy link
Contributor

@yegor256 Nice idea, thanks

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

No branches or pull requests

2 participants