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

Abstract dependencies with effects #188

Closed
leonp-s opened this issue Jul 31, 2023 · 5 comments
Closed

Abstract dependencies with effects #188

leonp-s opened this issue Jul 31, 2023 · 5 comments

Comments

@leonp-s
Copy link

leonp-s commented Jul 31, 2023

I am currently trying to figure out how to use abstract dependencies as deps to effects (and if this is possible / a good idea...), take for example,

struct Base {
    virtual ~Base() = default;
    virtual bool Load() = 0;
}

using Effect = lager::effect<Action, lager::deps<Base *>>;

this results in the following error,

no viable conversion from returned value of type 'std::pair<Model, lager::effect<std::variant<... Actions>, lager::deps<Base *>>>' to function return type 'std::decay_t<const Model &>' (aka 'Model')
        return new_model;

having read though the docs on effects I am struggling to understand why this might be an issue, any help would be really appreciated.

@arximboldi
Copy link
Owner

I think that should really be fine. Can you show me the rest of the code?

I use abstract dependencies often. When I do, I don't use Base* but Base&. Either way, I don't think there is anything that should prevent the pointer from working. Maybe the mistake is somewhere else?

@leonp-s
Copy link
Author

leonp-s commented Jul 31, 2023

Thanks for getting back to me!

Currently it is looking like this,

class TabsControllerDelegate
{
public:
    virtual ~TabsControllerDelegate () = default;
    virtual bool LoadTab (const std::string & tab_name, bool animate) = 0;
};

struct LoadTabAction
{
    std::string tab_name;
};

struct TabLoadedAction
{
    std::string tab_name;
};

using TabsAction = std::variant<LoadTabAction, TabLoadedAction>;
using TabsEffect = lager::effect<TabsAction, lager::deps<TabsControllerDelegate &>>;
using TabsResult = std::pair<TabsModel, TabsEffect>;

TabsResult UpdateTabs (TabsModel tabs_model, TabsAction tabs_action);

And being used as follows,

MockDel del;
auto store = lager::make_store<TabsAction> (TabsModel {},
                                            lager::with_manual_event_loop {},
                                            lager::with_deps (std::ref (del)),
                                            lager::with_reducer (UpdateTabs));

resulting in this error,

error: no viable conversion from returned value of type 'std::pair<TabsModel, lager::effect<std::variant<LoadTabAction, TabLoadedAction>, lager::deps<TabsControllerDelegate &>>>' to function return type 'std::decay_t<const TabsModel &>' (aka 'TabsModel')
        return new_model;

Again thanks for the help, I am sure I am doing something wrong, however when I change the dependency to something concrete I am able to build fine, so not too sure what might be going wrong.

I am trying to figure out how to go about testing effects. I am not sure if this might be going in the wrong direction anyway.

@arximboldi
Copy link
Owner

I think the problem is in the implementation of UpdateTabs, you are probably doing return new_model instead of return {new_model, lager::noop}, or even better return lager::result instead of std::pair...

@leonp-s
Copy link
Author

leonp-s commented Aug 1, 2023

Hi, thanks again for the response. Things appear to be resolved when using the following,

lager::with_deps (
                std::reference_wrapper<TabsControllerDelegate> (tabs_controller_delegate_mock))

@leonp-s leonp-s closed this as completed Aug 1, 2023
@arximboldi
Copy link
Owner

I do not think that's the issue, as that's equivalent to using std::ref like in your example above.

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

2 participants