-
-
Notifications
You must be signed in to change notification settings - Fork 193
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
Converting Typed Arrays to Variant Arrays Panics #727
Comments
there's a constructor we can use to turn arrays into a differently typed array This adds a constructor of |
Can i also ask for making Also there might be problem with |
Please avoid external projects unless absolutely necessary. This requires extra hurdles for anyone who wants to participate in the discussion. Instead, write a short code snippet demonstrating the relevant problem (and only that). Thanks! 🙂
What do you mean concretely with supertype in Rust? That they can be convertible?
Godot's relation between There were some great ideas here to address some of these issues, but it brings complexity that may not be justified, and it's not yet clear in which direction Godot is heading -- also with regards to typed dictionaries. |
Yes, as in typed array can be assigned into
Thanks for the link! But at least for ergonomic and coherence, it's best to copy Godot's behavior as it is currently. Like OP said, it's impossible to handle typed arrays without cast for every element type. That's kinda the reason why i'm using typed array only sparingly and read-only. Making |
The following is an example of the problem. The function #[func]
fn reveal_error(array: Variant) {
if array.get_type() == VariantType::ARRAY {
let converted = array.to::<Array<Variant>>(); // Or <VariantArray>
godot_print!("Array contents: {}", converted);
}
} Here is an example of the function being used in GDScript. The first call will pass fine, while the second one will panic. Working around this with an API in a convenient manner is extremely difficult. var untyped_array: Array = ["Hello", "World"]
var typed_array: Array[String] = ["Hello", "World"]
MyStruct.reveal_error(untyped_array)
MyStruct.reveal_error(typed_array) |
After further testing, it seem the problem is even more deeper than typed -> untyped conversion. For example this code: #[func]
fn dict_error(v: Variant) {
println!("{}", v.to::<Dictionary>());
} Cause panic-in-panic if fed typed array. From what i see, the problem goes like this:
|
If we allow
We'd also need to see how this affects Note It's important that I mentioned "input", not "write". More precisely, operations where Mutating methods such as This reduces type safety quite a bit, but maybe it's an interim solution until we find a design that models this better. For example, providing separate |
the solution i suggested above uses an array-constructor to construct a new untyped array from the old one. it doesn't merely treat the old array as a |
Thanks for clarifying, makes sense. There are probably use cases for both 🤔 GDScript also allows handling Since we can implement generic functions on |
i think having a workaround to doing a match over every single array type when wanting to convert a |
I did some impl here: https://github.com/Dheatly23/gdext/tree/variant-array-supertype Making fallible writes also has an advantage with read-only array. I only started testing it, so i don't know much about it's full implication. |
i feel like we shouldn't just make maybe we could do like We could with this allow a We could possible use the names |
Passing a typed
Array
from Godot and attempting to convert it to aVariantArray
currently panics (in a very unclear manner).This is unintuitive and unhelpful behavior, and makes dealing with Variants that could be typed Arrays extremely annoying, as you'd need to check against every possible contained type.
Attached is a minimum viable reproduction that shows off the issue well.
array_test.zip
I apologize if there's an intended way around this already (or if it's simply not intended), I wasn't able to find any workaround that didn't involve matching to each contained type.
The text was updated successfully, but these errors were encountered: