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

support JUST(may_expr, error_msg) #5855

Closed
lixinqi opened this issue Aug 12, 2021 · 6 comments · Fixed by #5904
Closed

support JUST(may_expr, error_msg) #5855

lixinqi opened this issue Aug 12, 2021 · 6 comments · Fixed by #5904

Comments

@lixinqi
Copy link
Contributor

lixinqi commented Aug 12, 2021

motivation:

Maybe<void> Foo() {
  CHECK_OR_RETURN(...) << Error::ErrorType0() << "...";
  CHECK_OR_RETURN(...) << Error::ErrorType2() << "...";
  return Maybe<void>::Ok();
}
Maybe<void> Bar() {
  const std::string local_var_bar;
  JUST(Foo()); // sometimes we want to record  `local_var_bar` to stackframe when Foo failed. 
  return Maybe<void>::Ok();
}

possible usages:

JUST(Foo(), "error: xxx");
JUST(Foo(), std::stringstream() << "error: xx");
JUST(TRY(Foo()),
    CATCH([&](const cfg::ErrorType0& error0) -> std::string { return "error: error0 xxx" }),
    CATCH([&](const cfg::ErrorType1& error1) -> std::string { return "error: error1 xxx" }))
@PragmaTwice
Copy link
Contributor

PragmaTwice commented Aug 14, 2021

I think we should simplify the JUST macro, for example:

JUST(foo<X, Y>(), error_msg<Z, W>())

Then we just cannot distinguish these two argument via __VA_ARGS__.

@PragmaTwice
Copy link
Contributor

A simple workaround: we give only a JUST(x) instead of JUST(...), and then it is the user's responsibility to wrap arguments, i.e.

JUST(foo<X, Y>()) // error
JUST((foo<X, Y>())) // ok

@lixinqi
Copy link
Contributor Author

lixinqi commented Aug 14, 2021

JUST(...) is more often used than error message appending.
How about refactoring TRY(...) to TRY(x)? i.e.

TRY(Foo<X, Y>()) // error
TRY((Foo<X, Y>())) // ok
const auto& maybe_val = TRY((Foo<X, Y>()), ErrorMsg<Z, W>()) // ok
JUST(maybe_val); // ok
JUST(TRY((Foo<X, Y>()), ErrorMsg<Z, W>()))

@PragmaTwice
Copy link
Contributor

PragmaTwice commented Aug 14, 2021

The current TRY seems to be a useless macro, but it was used in some source code.

#define TRY(...) __MaybeErrorStackCheckWrapper__(__VA_ARGS__)

I think we can remake TRY (or use another name like JUST_WITH_ERROR_MSG) to do this:

#define JUST_WITH_ERROR_MSG(val, msg) <`JUST(val)` with a error message `msg`> // or JUST_WITH_ERROR_MSG(val, ...)

JUST(foo<x, y>()) //ok
JUST_WITH_ERROR_MSG(foo<x, y>(), msg) // error!
JUST_WITH_ERROR_MSG((foo<x, y>()), msg) // ok 

@PragmaTwice
Copy link
Contributor

PragmaTwice commented Aug 14, 2021

BTW, I think CHECK_JUST and JUST are not good names, and users cannot distinguish these two macros by their names (the word CHECK is meaningless, and can lead to misunderstanding).
A better alternative is like GET_VALUE_OR_ABORT and GET_VALUE_OR_RETURN_ERROR.

@lixinqi
Copy link
Contributor Author

lixinqi commented Aug 14, 2021

Maybe is an analog of std::Result in rust (https://doc.rust-lang.org/std/result/enum.Result.html) whilst the names Maybe and JUST come from haskell (https://wiki.haskell.org/Maybe) .
Why not call it Result?Because I think neither rust nor haskell name them correctly. std::Result should be called Maybe in rust while Maybe should be called Optional.

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.

2 participants