fubark / cyber

Fast and concurrent scripting.
https://cyberscript.dev
MIT License
1.16k stars 38 forks source link

Some more if/else weirdness #41

Closed sts10 closed 1 year ago

sts10 commented 1 year ago

I was refactoring a function and found some weirdness with if/elsebranches in afor` loop.

In the playground, this outputs nothing.

my_arr = [4,5,6,7,8]
what_i_found = find_6_or_7(my_arr)
print 'Found: {what_i_found}' 

func find_6_or_7(an_arr):
    for an_arr each item:
        if item == 6:
            return 6
        else item == 7:
            return 7

But if I change the else to a second if, it runs as expected:

my_arr = [4,5,6,7,8]
what_i_found = find_6_or_7(my_arr)
print 'Found: {what_i_found}' 

func find_6_or_7(an_arr):
    for an_arr each item:
        if item == 6:
            return 6
        if item == 7:
            return 7

Using 2 ifs in this particular example isn't any less efficient than a if and else, but I think there are cases where it would be.

This time, I checked: I use all spaces for indents here.

fubark commented 1 year ago

This should be fixed in 3343587e2ae6a0e9fdfa7e46d3f620222288f7cb.