question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Or() not providing access to facts in Let() assignment

See original GitHub issue

Hi

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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
snikolayevcommented, Jan 4, 2018

@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

When()
    .Or(x => x
        .Match<A>()
        .Match<B>());

That is literally the same as having these two rules:

When()
    .Match<A>();
When()
    .Match<B>();

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.

1reaction
snikolayevcommented, Dec 31, 2017

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 always null, but the other one is always not null, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found