FirelyTeam / firely-cql-sdk

BSD 3-Clause "New" or "Revised" License
31 stars 17 forks source link

A `let` cannot refer to an alias of a let that comes before it in the same scope #199

Closed ewoutkramer closed 8 months ago

ewoutkramer commented 9 months ago

In CMS AntidepressantMedicationManagementFHIR.cql we find the following (correct) snippet of CQL:

define "Denominator Exclusions":
  Hospice."Has Hospice Services"
    or exists ( ( Status."Active Medication" ( ["MedicationRequest": "Antidepressant Medication"] ) ) ActiveAntidepressant
        let dosage: singleton from ActiveAntidepressant.dosageInstruction,
        timing: dosage.timing,
        boundsPeriod: timing.repeat.bounds,
        ActiveMedPeriod: boundsPeriod Meds

note that there is a let statement that first introduces dosage and then timing that uses dosage. This is useful and legal, but our current code batches up all lets in the same scope, and then adds them to the scope at the same time, which means that referring to a previous let in the same scope does not work.

I have fixed this in SingleSourceQuery by having each let introduce a new scope, with the subsequent lets defined in nested scopes. This is actually the formal way this is done in ML-style languages, so we might leave it at that.

EvanMachusak commented 9 months ago

In older HEDIS measures we used to do this, and at one time we supported it. I must have broken it at some point.