FHIR Chat · Where does the $this come from? · fhirpath

Stream: fhirpath

Topic: Where does the $this come from?


view this post on Zulip Tilo Christ (Jan 09 2022 at 16:54):

I am trying to figure out the proper execution of this test case Patient.name.first().subsetOf($this.name). In this expression, what is populating $this? The fact that its name is accessed seems to suggest that the entire patient should be in $this, but I cannot find any documentation that would suggest that $this = %context?

view this post on Zulip Brian Postlethwaite (Jan 09 2022 at 21:51):

$this is definitely not %context

view this post on Zulip Brian Postlethwaite (Jan 09 2022 at 21:52):

My understanding is that it should be the name value, but I'll take a closer look.

view this post on Zulip Brian Postlethwaite (Jan 09 2022 at 21:53):

(I.e. I don't understand what it's doing here or if it's valid)

view this post on Zulip Brian Postlethwaite (Jan 09 2022 at 22:12):

I think it's invalid.

view this post on Zulip Brian Postlethwaite (Jan 09 2022 at 22:13):

As $this represents a currently processing item in the evaluation of a function that takes an expression. Which in this case would be the name is the value, not the patient.

view this post on Zulip Brian Postlethwaite (Jan 09 2022 at 22:16):

%context is the start point for the entire evaluation, also note in the fhir spec the %resource and %root Resource which are also closely related.

view this post on Zulip Brian Postlethwaite (Jan 09 2022 at 22:18):

image.png

view this post on Zulip Tilo Christ (Jan 09 2022 at 22:35):

thanks, maybe I expressed myself a bit unclear. I am aware that %context and $this are two different things. I am using $this in expressions of aggregator and .where and understand how it holds the currently iterated item. For this subsetOf example (again taken from the FHIRPath test suite) I don't understand what it should be populated with and because of which part of the spec. Since the author of the test is trying to access a .name property on it, I think it must somehow be the patient, but I don't get what would make the patient go into $this. In the fhirpath.js this test passes. When I rewrite it to Patient.name.first().subsetOf(%context.name) it will also pass.

view this post on Zulip Brian Postlethwaite (Jan 10 2022 at 00:41):

Maybe it's accidentally passing as comes out with an empty set or something.

view this post on Zulip Tilo Christ (Jan 12 2022 at 17:31):

I'm still trying to get a full understanding of the proper application of $this. If I have the expression (0|1).iif($this = 0, $index, false), what should be the proper way to think about this? Should $this be unpopulated, because iif is not designed to iterate over an input collection? Or should iif be executed 2 times, each time providing a single item (0, and 1) in $this, and the corresponding index in $index? Or put (0|1) into $this and set $index to 0? I'm trying to find the answer from reading the spec and not just by executing fhirpath.js and then doing whatever it does, without understanding why (fhirpath.js puts [0, 1] into $this, and empty collection into $index).

view this post on Zulip Tilo Christ (Jan 14 2022 at 17:47):

Sorry, still trying to get any input on the proper handling of $this. I have thus far tried several implementations of FHIRPath, they all behave different, and I'm really unclear how to read the spec. When I do something like Patient.name.iif(true, $this, 0), I have seen the following behaviours:

  • engine signals error
  • engine puts a list of the names into $this
  • engine puts the entire patient resource into $this

The FHIRPath test suite contains tests which imply that the last option (entire patient) is the right one, but I have only seen this behaviour on the HAPI FHIRPath engine, and cannot find something about this in the spec.

view this post on Zulip Lloyd McKenzie (Jan 14 2022 at 20:54):

@Grahame Grieve @Bryn Rhodes @Paul Lynch

view this post on Zulip Bryn Rhodes (Jan 14 2022 at 23:17):

I agree that's pretty unclear in the spec. The section discussion says "The functions in this section operator on collections with a single item", which makes sense for all the other functions in that section (conversion functions), but iif isn't really a conversion function, it's more a conditional operator, so it's not clear that that applies. The examples in the spec of iif are all stand-alone (i.e. not invoked using the . notation), which is how I have always used iif. So I would expect Patient.name.iif(true, $this, 0) to be invalid, I would write Patient.name.select(iif(true, $this, 0), and then the $this is clearly iterating over the names.

view this post on Zulip Tilo Christ (Jan 15 2022 at 00:14):

@Bryn Rhodes that makes a lot of sense. The other item that I'm still trying to figure out is a test case from the test suite, also related to $this. It is this one: Patient.name.supersetOf($this.name.first()) = true. For this one, the HAPI FHIRPath engine puts the Patient resource into $this, (and also in the iif-example I gave, it is the engine that put the entire patient into $this)). Is there any explanation from the spec for that behaviour, or is this in your mind also something that should behave differently or not at all?

view this post on Zulip Bryn Rhodes (Jan 24 2022 at 16:50):

@Tilo Christ the spec says that $this is only used when the function takes an expression as a parameter, and supersetOf explicitly takes a collection (i.e. it's not an interating operator). So I think this test is, based on the spec, invalid. It seems like the implementations took the approach of considering the root evaluation an "iteration" context so that $this would be available, but I don't see any language in the spec that supports that.


Last updated: Apr 12 2022 at 19:14 UTC