Q – unpredictable global vars
The behaviour of the Q language can often be unpredictable and unintuitive, leading to numerous ways to shoot yourself in the foot. I think every developer has a piggy bank with such cases. Here’s one example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
q) f: { a: 1; b,: 2 } q) f[] q) a 'a [0] a ^ q) b ,2 q) get `. f| {a: 1; b,: 2} b| ,2 |
As you can see, we defined function with 2 local variables, but variable b suddenly became global.
The interesting thing is if we already have global variable “b” set, it will be affected anyway, unexpected appearance/amendment of the global vars can be an unfortunate surprise. So the best way – have it initialised inside of the function.
31 Jul 2024 Alex Ternovoy 272