Or() not providing access to facts in Let() assignment
See original GitHub issueHi
I am unable to use the facts within the let()
operator when I am Matching on those facts within an Or()
grouping.
Example:
public override void Define()
{
Foo foo = null;
Bar bar = null;
bool result = false;
When()
.Or(x => x
.Match(() => foo, c => c.MyProperty.HasValue)
.Match(() => bar, b => b.MyProperty.HasValue)
)
.Let(() => result, () => (foo.MyProperty ?? false) || (bar.MyProperty ?? false));
Then().Yield(_ => new FooBar(result));
}
In this example both foo
and bar
are null when inspecting them at the assignment stage of the Let()
operator.
However, if I remove the .Or()
grouping it will work.
Hope that makes sense
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Ansible Facts and How to use them - Ansible Variable list
We are going to start from the ansible Gathering facts. As ansible collected facts are considered as the ansible playbook variables (or) facts....
Read more >Using Variables
Many registered variables (and facts) are nested YAML or JSON data structures. You cannot access values from these nested data structures with the...
Read more >Add or change a table's primary key in Access
An Access primary key in an Access database table is a field with unique values ... key will no longer provide the primary...
Read more >Var, Let, and Const – What's the Difference?
In this article, we'll discuss var , let and const with respect to their ... So we cannot access the variable hello outside...
Read more >Guidance on Constitutionally Protected Prayer and ...
There is no specific Federal form that an LEA must use in providing this certification to its SEA. The certification may be provided...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@nagoh - yes, it’s the same reason. Basically, different branches of an OR group are completely independent from each other. If you have a rule
That is literally the same as having these two rules:
Obviously, in case of the two independent rules, the yield statements are independent. So, they are also independent in case of an OR group. I agree that the OR group is not very intuitive as currently implemented. Currently, it’s mostly just a mechanism to help avoid copy-pasting portions of a rule, and does not follow general intuitions associated with logical disjunctions.
Hi @nagoh I created a test with this scenario, and I think it will help explain what’s going on. So, what you are probably observing in the
Let
operator is that one of the facts is alwaysnull
, but the other one is always notnull
, Even if both foo and bar are present,Let
expression will evaluate twice - once with foo null, and once with bar null. The reason is that OR group is compiled into several rules behind the scenes, so each branch of the OR group becomes its own individual path in the Rete network. So,Let
binding will be present in each of those paths, but obviously, one path will only apply to foo, and the other path will only apply to bar. In general, when using OR groups, you should expect that facts pertaining to one of the OR branches will be null. When the rule actually executes (fires), all those paths are coalesced together, so there is this illusion that sometimes all facts are present. You should not rely on this and, as I said, generally should expect that some of the facts may be null. Let me know if this helps.