qore icon indicating copy to clipboard operation
qore copied to clipboard

auto inconsistencies - first assignment to 'auto' declared varible should define its type

Open sejvlond opened this issue 6 years ago • 1 comments

I thought that first time assignment into auto declared variable declares its type which is then preserved, except for cases when type cannot be deducted (hash with values as strings, ints, floats, ...) then the type is deducted as simple 'any'

Is that right? Because when hash<auto> is defined, it behaves exactly that way, but when simple variable is defined as auto it behaves like it is any. See examples below, where I think the first one should behave exactly the same as the second one.

[~] $ qore -ne 'auto i = "string"; i = 3;'

[~] $ qore -ne 'string i = "string"; i = 3;'
unhandled QORE System exception thrown in TID 1 at 2019-02-20 20:44:27.371289 Wed +01:00 (CET) at <command-line>:1 (Qore builtin code)
PARSE-TYPE-ERROR: lvalue for assignment operator '=' expects type 'string', but right-hand side is type 'int'

[~] $ qore -ne 'hash<auto> h = {"i": "string"}; h.i = 3;'
unhandled QORE System exception thrown in TID 1 at 2019-02-20 20:44:48.840423 Wed +01:00 (CET) at <command-line>:1 (Qore builtin code)
RUNTIME-TYPE-ERROR: <lvalue> expects type 'string', but got type 'integer' instead 

sejvlond avatar Feb 20 '19 19:02 sejvlond

it's way more complicated then I thought, see:

$ qore -ne 'list<auto> l; l += "string"; l += 1;'
$ qore -ne 'list<auto> l = ("string"); l += 1;'
unhandled QORE System exception thrown in TID 1 at 2019-03-25 14:04:16.842124 Mon +01:00 (CET) at <command-line>:1 (Qore builtin code)
PARSE-TYPE-ERROR: lvalue for assignment operator '=' expects type 'list<auto>', but right-hand side is type 'string'

the same with hashes

$ qore -ne 'hash<auto> h; h.a = 1; h.b = "string";'
$ qore -ne 'hash<auto> h = {"a" : 1}; h.b = "string";'
unhandled QORE System exception thrown in TID 1 at 2019-03-25 14:05:27.372612 Mon +01:00 (CET) at <command-line>:1 (Qore builtin code)
RUNTIME-TYPE-ERROR: <lvalue> expects type 'int', but got type 'string' instead

sejvlond avatar Mar 25 '19 13:03 sejvlond