FirstGearGames / FishNet

FishNet: Unity Networking Evolved.
Other
1.39k stars 149 forks source link

Incorrect enumerator return type on SyncList #657

Closed john-ofg closed 7 months ago

john-ofg commented 7 months ago

General Unity version: noticed in 2022.3, but should be present in any Unity version Fish-Networking version: 4 and up Discord link: None

Description The return type of the public GetEnumerator() method in SyncList returns the non-generic IEnumerator instead of the generic IEnumerator<T>. This makes type of e in foreach (var e in syncList) an object, which is inconvenient in codebases that use var. This grows into a larger issue when explicitly specifying the iteration type of a SyncList of any value type: foreach (ValueType e in syncList) results in a hidden boxing conversion of object to ValueType at each iteration.

Replication

private readonly SyncList<int> list = new();

public void Example() {
    foreach (var e in list) { // e is boxed as object, which is an unnecessary allocation
        int x = e + 1; // This will not compile because e is boxed as an object
    }
}

Expected behavior Ideally, the public GetEnumerator() of SyncList would return IEnumerator<T>: public IEnumerator<T> GetEnumerator() => Collection.GetEnumerator();

I have been making this change in my projects since updating to FishNet 4 and it works well. Thank you for making this excellent library!

FirstGearGames commented 7 months ago

Ty! resolved in 4.2.1