Unexpected compilation error with if() and local vars


Based on the local variables docs and the regexp capturing docs, I wrote a little statement to extract an interesting part of our request URLs for later use:

set var.zoom = if(req.http.x-normalized-url ~ "/(\d{1-2})/\d+/\d+\.[^/]*$", std.atoi(re.group.2), 0);

But this seems to fail with a compilation error:

Expected CNUM got 'if'
(program line 148), at
(input Line 356 Pos 18)
  set var.zoom = if(req.http.x-normalized-url ~ "/(\d{1-2})/\d+/\d+\.[^/]*$", std.atoi(re.group.2), 0);

It looks as though the compiler is expecting a number on the right side of the set command and if isn’t satisfying that requirement.

When I rewrote the single-line if like so:

  if (req.http.x-normalized-url ~ "/(\d{1-2})/\d+/\d+\.[^/]*$") {
    set var.zoom = std.atoi(re.group.2);

…it compiled without problem.

Is this a bug in the compiler? Or am I misunderstanding something?


Currently, the if() function only returns STRING types, and thus doesn’t work with non-string variables.

So it’s not really a bug, it’s more of a lack in features. I’ll have a chat with someone working on the compiler.


Internal ticket for this issue has been created.



I can understand that if() would only return string for now. At the very least the documentation that mentions it should include that fact.


It does actually say

Implements a ternary operator for strings



It does indeed! Apologies.