Add iteration methods to Dictionary by lilizoey · Pull Request #99 · godot-rust/gdext (original) (raw)

Thanks a lot for this pull request! Very nice to see the API grow 🙂


Now we have:

keys_array      -> Array
values_array    -> Array
iter_shared     -> Iter over (Variant, Variant)
iter_shared_as  -> Iter over (K, V)
keys_shared     -> Iter over Variant
keys_shared_as  -> Iter over K

But we don't have an iterator over values. If we added that, we would have 8 methods already. This adds quite a bit of API surface, and our "shared" naming convention has the potential to make things rather unreadable 😬

I wonder if it's possible to have a generic iterator adapter over these 🤔

dict.keys_shared()                  -> Iter over Variant
dict.keys_shared().typed::<K>()     -> Iter over K
dict.iter_shared().typed::<K, V>()  -> Iter over (K, V)
...

This could potentially be reusable for arrays as well.

We could even consider .array() or .collect::<Array>(); not sure how much indirection it would add. But generally it's really nice to have such composable building blocks 🏛️

Also, I'd start with a panicking adapter that directly returns the elements, rather than delivering Result<...> -- the Result is mostly useful if you want to extract the result out of iteration and combine with ?, and I'd guess that many people would just call try_from_variant() in that case.