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

force_opt combined with bind_opt results in compilation error #215

Closed
henri-v opened this issue Nov 11, 2024 · 6 comments
Closed

force_opt combined with bind_opt results in compilation error #215

henri-v opened this issue Nov 11, 2024 · 6 comments

Comments

@henri-v
Copy link

henri-v commented Nov 11, 2024

Creating a lens with force_opt as the innermost operation and then lifting this lens using bind_opt causes a compilation error. For example, adding the following code in test/type_erased_lens.cpp will not compile:

TEST_CASE("lenses, force_opt, type erasure, bind_opt")
{
    struct Foo { int i; };
    //const lager::lens<Foo, std::optional<int>> opt_member_lens =
    const auto opt_member_lens =
        attr(&Foo::i) | force_opt;
    const auto bound_opt_member = bind_opt(opt_member_lens);
    CHECK(view(bound_opt_member, std::optional{Foo{3}}) == std::optional{3});
    CHECK(view(bound_opt_member, std::optional<Foo>{}) == std::nullopt);
}

Error message from GCC 11.4.0

By turning the force_opt lens into a type-erased lens, the code will compile, so there should be nothing wrong with the logic of the code. This can be used as a workaround.

It is possible to get the same result with map_opt. However, in some cases it’s better for code organisation that these steps are in separate functions, which can be combined more freely.

@arximboldi
Copy link
Owner

Ping @Siapran

@Siapran
Copy link
Contributor

Siapran commented Nov 12, 2024

ok, two ways to fix this:

  • change auto here to be the actual optional type (cleanest imo)
  • explicitly construct type of whole here from the value of part

either way, I really ought to rewrite a lot of this.

@arximboldi
Copy link
Owner

Whatever you chose @Siapran sounds good, I trust you on this and would very much appreciate a PR if you have the time!

@Siapran
Copy link
Contributor

Siapran commented Nov 12, 2024

I'll try to do that today if whatever I caught on the flight back home will let me 👍

@Siapran
Copy link
Contributor

Siapran commented Nov 13, 2024

... I really should just rewrite the whole thing sooner than later. there's some really dubious (if not straight up wrong) moves going on in there.

@arximboldi
Copy link
Owner

Fixed: #216

Thank you so much @Siapran !

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

No branches or pull requests

3 participants