Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any type not working with arithmetic types #498

Open
ZekeMedley opened this issue Jul 29, 2019 · 1 comment

Comments

@ZekeMedley
Copy link
Member

commented Jul 29, 2019

Presently, something like this works fine in Zeek.

local a: any = "fast";
local b: string = a;
print b + " dog";   # prints -> fast dog

But does not work for arithmetic types.

local a: any = 1;
local b: count = a;
print b + 10;

Error assignment of non-arithmetic value to arithmetic (count/any) (b = a)

One result is that Zeek's Queue stops working for arithmetic types.

local q = Queue::init();
Queue::put(q, 1.1);
Queue::put(q, "do");
local a: double = Queue::get(q);
local b: string = Queue::get(q);

My suspicion is that fixing this should just be a matter of changing a little logic here:

zeek/src/Expr.cc

Lines 2220 to 2230 in 95ce177

bool AssignExpr::TypeCheckArithmetics(TypeTag bt1, TypeTag bt2)
{
if ( ! IsArithmetic(bt2) )
{
char err[512];
snprintf(err, sizeof(err),
"assignment of non-arithmetic value to arithmetic (%s/%s)",
type_name(bt1), type_name(bt2));
ExprError(err);
return false;
}

@0xxon 0xxon changed the title Any type ot working with arithmetic types Any type not working with arithmetic types Jul 29, 2019

@jsiwek

This comment has been minimized.

Copy link
Member

commented Jul 29, 2019

Don't immediately have an answer for how the language should handle any here. That type is generally probably not consistently handled and probably most often used just in cases where we wanted to bypass type-checking... not something we intend people to do generally (guess would have just made a dynamically typed language then).

But you can be explicit about your type conversions as a workaround for you examples:

local a: any = 1;
local b: count = a as count;
print b + 10;
local q = Queue::init();
Queue::put(q, 1.1);
Queue::put(q, "do");
local a: double = Queue::get(q) as double;
local b: string = Queue::get(q) as string;
print a, b;

For queue, also don't generally think they were intended to be used to contain heterogenous value types, so that seems like it may be mostly up to the user to keep track of themselves if they're going to use it that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.