Consider this snippet:
function f(x: any)
{
switch (x)
{
case type any:
print "any";
break;
case type vector of any:
print "vector of any";
break;
}
}
event bro_init()
{
f(42);
}
I would have imagined that the two cases are considered different types, but Bro doesn't think so:
error in ./test.bro, lines 9-10: duplicate case label (case type vector of any:{ print anybreak })
Adding a type alias for either type doesn't change the error message.
I am aware that this is a somewhat pathological case, because 'case type any' is probably equivalent to the 'default' case.
Matthias
A 'vector of any' also qualifies as an 'any', so while the error
message of "duplicate case label" could possibly be improved, I think
it's still correct in that it is meant to prevent an ambiguous case
match. In your example, if you did pass in a vector to that function,
it could choose either the 'any' case or the 'vector of any' case.
One might have the expectation that it chooses the first case that
matches in top-down order, however that might conflict with other
expectations that switch statements can be implemented via hash table
lookup and require unambiguous cases. In Bro, regular 'case' is
currently a hash table lookup, while actually 'case type' is a linear
search, but that seems to be just implementation detail and not a
conscious decision that can be relied on to allow ordered-case-type
matching.
- Jon
A 'vector of any' also qualifies as an 'any', so while the error
message of "duplicate case label" could possibly be improved, I think
it's still correct in that it is meant to prevent an ambiguous case
match.
Yeah, with that reasoning anything qualifies as "any", so adding "any"
should never show up in a "case type" statement. Probably a better way
to handle "any" is by just using the "default" case.
Matthias