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

Allow foreach on 0-indexed arrays #2811

Closed
wants to merge 1 commit into from
Closed

Conversation

Denneisk
Copy link
Member

@Denneisk Denneisk commented Oct 26, 2023

A small minority of E2 functions return 0-indexed arrays. This allows one to foreach over those arrays, which is at least a convenience improvement over a for loop.

Modifies iter operator to return -1 as its third argument if an array has an element at 0.

Test code

Arr = array(1, 2, 3, 4, 5, 6)
Arr[0, number] = 0

Str = array()
foreach(K:number, V:number = Arr)
{
    Str:pushString(format("%d : %d", K, V))
}

print(Str:concat("\n"))

@Vurv78
Copy link
Contributor

Vurv78 commented Oct 26, 2023

I don't think this should be encouraged / supported. As you said it's a small minority of functions

It's not a big perf hit like supporting non sequential arrays since this would only be run once, but that isn't really the issue here anyway.

@Vurv78
Copy link
Contributor

Vurv78 commented Oct 26, 2023

I'd recommend creating RFC discussions prior to making a PR to make any not objectively better changes

@Denneisk
Copy link
Member Author

It seemed objectively better in my sight which is why I didn't, actually. If functions can return 0-indexed arrays, why can't foreach support that? especially since the work required to do so is so small.

Either way, the PR is small. I probably won't lose sleep if it's denied.

@thegrb93
Copy link
Contributor

I don't think this should be encouraged / supported. As you said it's a small minority of functions

It's not a big perf hit like supporting non sequential arrays since this would only be run once, but that isn't really the issue here anyway.

High speed components use 0 index so it makes sense to be supported.

@Vurv78
Copy link
Contributor

Vurv78 commented Oct 26, 2023

I don't think this should be encouraged / supported. As you said it's a small minority of functions
It's not a big perf hit like supporting non sequential arrays since this would only be run once, but that isn't really the issue here anyway.

High speed components use 0 index so it makes sense to be supported.

What does this have to do with arrays, though

@Denneisk
Copy link
Member Author

Denneisk commented Oct 26, 2023

What does this have to do with arrays, though

Wiremod has (or had) an odd propensity to start arrays at 0, so it should be at least anticipated for.

@Vurv78
Copy link
Contributor

Vurv78 commented Oct 26, 2023

This has nothing to do with the E2 data type "array" though. And wiremod outside of E2 hardly ever returns or takes arrays.

My philosophy is to keep E2 isolated as much as possible from the rest of wiremod.

Trying to make E2 behave like the rest of wiremod and not like a programming language is what got E2 into runOn* and three unary operators for wiremod specific behavior (~, -> and partially $)

@thegrb93
Copy link
Contributor

thegrb93 commented Oct 26, 2023

I don't think this should be encouraged / supported. As you said it's a small minority of functions
It's not a big perf hit like supporting non sequential arrays since this would only be run once, but that isn't really the issue here anyway.

High speed components use 0 index so it makes sense to be supported.

What does this have to do with arrays, though

I guess if you have an array of data you want to write to a high speed component, although I think there are writearray methods or something already for that. Not really sure of other uses so it does seem niche.

@Divran
Copy link
Contributor

Divran commented Oct 26, 2023

Anything in gmod that returns arrays starting at zero is implemented wrong. Lua is a 1-indexed language and trying to force it not to be is a completely unecessary mistake.

As far as I know there's no such function in wiremod and never has been... with the possible exception being a very small amount of functions that just return the output of a gmod function without changing anything. There are a few functions in gmod itself that do return arrays starting at zero but like I said I definitely consider those to be implemented wrong.

@Denneisk
Copy link
Member Author

Well, I concede. I'll look into seeing if 0-indexed array returns in E2 can (or should) be converted to 1-indexed.

@Denneisk Denneisk closed this Oct 27, 2023
@Denneisk Denneisk deleted the array0 branch November 16, 2023 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants