Assigning actions to a variable

≯℡__Kan透↙ 提交于 2020-01-16 05:35:08

问题


In answering Aaron's recent question, I'd like to do something like the following:

rule first_rule {
    select when pageview "exampley.com/\?name=(.*)" setting (username)
    pre {
        isjoe = username eq "joe";
        myaction = defaction() {
            thisaction = isjoe => notify("Hello, World", "Hi there, Joe!") | noop();
            thisaction();
        };
    }
    {
        notify("Will it work?", "Methinks you are #{username}");
        myaction();
    }
}

However, the defaction never seems to work. It doesn't like that I'm trying to assign an action to a variable and then return that variable.

What am I doing wrong?


回答1:


You are really close.

You can't call an action till the end of the defaction. You need to create a defaction that delays execution till the right time.

Change:

thisaction = isjoe => notify("Hello, World", "Hi there, Joe!") | noop();

to

thisaction = isjoe => defaction(){notify("Hello, World", "Hi there, Joe!");} | noop;

Note the added defaction and I removed the parens from noop.

This concept is is similar to javascript closures.



来源:https://stackoverflow.com/questions/5888569/assigning-actions-to-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!