﻿`` R := {A:3.5, B:"RB"};
`` T := Range(10)->{A:it / 2, B:ToText(it) };

// Reference global.
module { var X from T; let Num := X->Count() }
###
With(
    M : module {
        param N := 2;
        var X from T->Take(N);
        let Num := X->Count();
    },
    (M, M with { N : 5 }))
###
// Reference outer scope.
Range(10)->ForEach(as n,
    module {
        var X from Range(n);
        let Num := X->Count()
    })
###
// Reference outer scope field.
Range(10)->{ A: it, B: ToText(it) }->ForEach(
    module {
        var X from Range(A);
        let Num := X->Count()
    })
###
// Reference both outer scope and global.
Range(10)->ForEach(as n,
    module {
        var X from T->Take(n);
        let Num := X->Count()
    })
###
// Reference both outer scope field and global.
Range(10)->{ A: it, B: ToText(it) }->ForEach(
    module {
        var X from T->Take(A);
        let Num := X->Count()
    })
###
// Two modules of the same type.
With(
    M1 : module { var X in T; let Y := X.A },
    M2 : module { var X := R; let Y := X.A + 1 },
    (M1, M2, [M1, M2]))
###
// Conversion from module to canonical record.
With(
    M : module { var X in T },
    RR : { X: R },
    (M, RR, [M, RR]))
###
// Conversion from module to non-canonical record.
With(
    M : module { var X in T; var Z from 0 to 100 },
    RR : { X: R, Y: 3.5 },
    (M, RR, [M, RR]))
