Search code examples
lasso-lang

At what point are query expressions invoked?


In the below example would the queryable #q be evaluated a second time?

local(max = 10, m = 5)

local(q = with n in 1 to 10 select #n+#m) 

#q // 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

if(true) => {
   #q // is it invoke here like the above?
}

I suspect not, but if that's the case is it #q->asString that invokes the query?


Solution

  • I think you're right. Here's a simple test calling a counter thread object that increments on each call.

    define test_count => thread {
        data private val = 0
        public asstring => {
          .val += 1
          return .val
        }
    }
    
    local(max = 10, m = 5)
    test_count
    
    local(q = with n in 1 to 10 select #n+#m + integer(test_count -> asstring))
    '<br />'
    test_count
    '<br />'
    #q
    '<br />'
    test_count
    '<br />'
    if(true) => {
       #q
    }
    '<br />'
    test_count
    

    Result ->

    1
    2
    9, 11, 13, 15, 17, 19, 21, 23, 25, 27
    13
    14

    The second call for #q is never processed. You can however force it to run by outputting it.