KRL:如果那么其他问题
我正在尝试编写类似于 If、else if、else 语句的内容。然而,在线编译器给我带来了问题。
我通常用 jquery 编写代码,然后发出它......但这次我尝试以 KRL 方式执行此操作,但遇到了问题。
当我编写如下内容(在 Pre 和 Post 块之间)时,出现编译器错误:
if (someExpression) then { //做一些代码 } 别的 { //做一些代码 我知道
这是有原因的...但我需要有人向我解释...或者向我指出文档。
I am trying to write something comparable to an If, else if, else statement. However, the online compiler is giving me problems.
I usually write my code in jquery and just emit it... but I am trying to do it the KRL way this time and I am running into problems.
When I write something like the following (between the Pre and Post blocks), I get compiler errors:
if (someExpression) then {
//Do some code
} else {
//DO some code
}
I know that there is a reason... but I need someone to explain it to me... or point me to the documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 KRL,通常最好有单独的规则来处理问题中描述的“if...then”和“else”情况。这只是因为它是一种规则语言;你必须改变你思考问题的方式,而不是通常的程序方式。
也就是说,迈克提出明确事件的建议通常是解决问题的最佳方法。下面是一个示例:
在这个简单的示例中,编写两个相同的规则也很容易,只是一个规则在
if
语句中包含一个not
,另一个规则在if
语句中包含一个not
。没有。这实现了相同的目的:在 Kynetx 文档。我也喜欢迈克在 Kynetx 应用一天。
With KRL, it's often best to have separate rules to handle the "if...then" and "else" cases described in your question. That's simply because it's a rule language; you kind of have to change your way of thinking about the problem from the usual procedural way of doing it.
That said, Mike's suggestion to raise explicit events is usually the best way to solve the problem. Here's an example:
In this simple example, it would also be easy enough to write two rules that are the same except that one has a
not
in theif
statement and the other does not. That accomplishes the same purpose:There is more documentation about the postlude (
fired
andnotfired
, for example), on Kynetx Docs. I also like the more extensive example that Mike wrote on Kynetx App A Day.您可以在 pre 块中使用三元运算符进行变量赋值,如 http://kynetxappaday.wordpress.com/2010/12/21/day-15-ternary-operators-or-conditional-expressions/
您还可以根据条件有条件地引发显式事件是否触发操作块,如 http://kynetxappaday.wordpress.com/2010/12/15/day-6-conditional-action-blocks-and-else-postludes/
You can use ternary operators in the pre block for variable assignment as illustrated at http://kynetxappaday.wordpress.com/2010/12/21/day-15-ternary-operators-or-conditional-expressions/
You can also conditionally raise explicit events based on whether the action block fired or not as illustrated at http://kynetxappaday.wordpress.com/2010/12/15/day-6-conditional-action-blocks-and-else-postludes/
以下是 Sam 发布的一些代码,解释了如何使用缺陷来模仿 ifthenelse 行为。这位天才的所有功劳都归功于萨姆·柯伦。这可能是您能得到的最佳答案。
Here is some code posted by Sam, that explains how to use defactions to mimic an ifthenelse behavior. All this credit for this genius belongs to Sam Curren. This is probably the best answer you could get.