These are chat archives for ChaiScript/ChaiScript

23rd
Feb 2015
Tomáš Mládek
@tmladek
Feb 23 2015 19:13
I'm having a bit of a trouble working with Vectors in ChaiScript
the following is okay:
var a = get_vector(); for (var i = 0; i < a.size(); ++i){ print(a[i]); }
but the following:
var a = get_vector(); for (var i = a.size()-1; i >=0; --i){ print(a[i]); }
throws a Can not find appropriate array lookup operator '[]'." With parameters: (Vector, size_t) error
Jason Turner
@lefticus
Feb 23 2015 19:35
that looks like a bug (it'd be great if you filed a report). In the meantime, you can work around it with: var a = get_vector(); for (var i = int(a.size()-1); i >=0; --i){ print(a[i]); }
you could also use retro
like:
for_each(retro(range(a)), fun(x) { print(x); })
Tomáš Mládek
@tmladek
Feb 23 2015 19:39
right, I thought I misunderstood something about vectors :) thanks! Where should I go about filling the report?
Tomáš Mládek
@tmladek
Feb 23 2015 19:39
okay, thanks!
Jason Turner
@lefticus
Feb 23 2015 19:40
the problem is that a.size() is a size_t and the [] operator is expecting an int. I have automatic conversions for numeric types, but it looks like somehow those conversions are not called for []
oh, I guess that makes a good point. If your code had worked as expected, you'd get an infinite loop
since size_t is unsigned, and 0 - 1 unsigned would be MAX_INT
you'd never break out of your loop condition
Tomáš Mládek
@tmladek
Feb 23 2015 19:42
oh!
that's still fairly unexpected though
Jason Turner
@lefticus
Feb 23 2015 19:43
the error you got is unexpected, true, but you'd have the same infinite loop if you did it in c++11: for (auto i = a.size() - 1; i >= 0; --i) { /* ... */}
difference being that the c++ compiler would have warned you
Tomáš Mládek
@tmladek
Feb 23 2015 19:43
but not for int i = a.size()-1, no?
hm, but that's just the way ChaiScript's typing works I suppose
do I still fill the issue?
Jason Turner
@lefticus
Feb 23 2015 19:45
yes, I should fix the bug. and correct, if you'd done int you would not get the infinite loop
This is fascinating to me, as a C++ geek:
Jason Turner
@lefticus
Feb 23 2015 19:50
you have to turn the warning level all the way up to -Wextra to get the compiler to tell you that you made an infinite loop
Clang complains at -Wall level
Tomáš Mládek
@tmladek
Feb 23 2015 19:55
well that's not how vectors should be iterated through anyway, I suppose otherwise the error would be a bit more prevalent and hence not a -Wextra level warning