Special case blank identifier _ in Zeek scripts

Hello,

we’ve been toying with the idea of reserving the blank identifier _ in Zeek scripts such that it can not be referenced anymore and therefore not used as a local identifier.

This would, besides other future use-cases, allow to support using _ and re-using it within the same scope to explicitly ignore loop variables similar to what may be known from Go or what is done conventionally in Python.

# Index, value vector iteration (coming with Zeek 5.1) ignoring the index
local vec = vector("a", "b", "c");
for ( _, v in vec )
    print v;

# Key, value iteration ignoring a subset of the key/index
local tab = table(["a",1,T] = "a1a", ["b",2,F] = "b2b", ["c",3,T] = "c3c");
for ( [_,c,_], v in tab )
    print c, v;

# Ignore all keys at once, only iterate over the values
for ( _, v in tab )
    print v;

# Poor man's strlen()
local str = "a string";
local len = 0;
for ( _ in str )
    ++len;

Above script would currently fail due to _ being typed according to its first occurrence and clashing with later usages.

On the flip side, _ will not be usable as a local identifier anymore. Outside of obfuscation exercises, this seems to take little away and we’re considering introducing this change without a deprecation/warning.

The following draft PR contains an initial set of changes - if you have thoughts/comments around this or had situations where ignoring loop vars would’ve been nice, happy to hear about it here, on Github or Slack.

Thanks,
Arne

1 Like