-
Notifications
You must be signed in to change notification settings - Fork 44
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
Conversions to and from hashmap #230
Comments
One more failing case is that when the list has duplicated name of entries (e.g. We might be able to have a one-way conversion by implementing only So, maybe it should be |
On the other hand, conversion between |
The conversion is not ideal for lists as it doesn't conserve the order either. But as Hiroaki says, environments do not preserve order either and take only the last duplicate assignment. I primarily added hashmaps for the case where you just wanted to take an anonymous list or env and map I hope to have a |
for the issue of ordering, in the case of named list conversion: is BTreeMap worth considering for this conversion, rather than hashmap? at least https://doc.rust-lang.org/std/collections/struct.BTreeMap.html#method.iter relevant extendr/extendr-api/src/robj/into_robj.rs Line 530 in be761f3
|
@brendanrbrown I feel you're raising a slightly different question. The purpose of extendr is to provide bindings, and the user should decide what data structure to use. So if somebody wants to work with hashmaps they should be able to do so, and if somebody wants to work with BTreeMaps they should also be able to do so. So by all means, yes, we should support BTreeMaps. But that's a different question than what the default choices should be for hashmaps. If you'd like to provide support for BTreeMaps you're welcome to add it. |
yes, good point |
Sorry, I think I was a bit confused at the time of writing the above comment. Let me summarize my thoughts here. In general, conversions can be categorized by the two characteristics
and it seems the sense is that we should use On the other hand, regarding
I think the problem is that these two are conflicting.
so, our choice can be:
and I'm inclined to accept the option 2. Option 1 should be technically possible, but I don't feel it's worth the effort. (I do want to document the general rule of our conversions. I'd appreciate your comments and suggestions on #244!) |
After reading the Rust RFC discussion, I'm thinking that option 1 is better. It makes the conversion explicit without being much more work. In this scenario, if I wanted to write a function that accepts a hashmap, I'd have to instead accept a list and then call an explicit constructor to convert the list to a hashmap. That's one additional line of code (so not a big burden) and it guarantees that I've at least thought about the fact that I'm converting a list to a hashmap. I would prefer the same approach when returning a hashmap back to R, as it would require me to think about whether I want to return a list or an environment. And in the case of environment, I'd probably also have to figure out and explicitly state what the parent env is. So all around a cleaner solution, for a minor cost in additional code. |
In other words, instead of writing this: #[extendr]
fn list_str_hash(x: HashMap<&str, Robj>) {
// do some computation on x
} we'd have to write something like this: #[extendr]
fn list_str_hash(y: List) {
let x = HashMap<&str, Robj>::from_list(y);
// do some computation on x
} And similarly when returning to R. I like the second option better. |
Thanks. So, what you are suggesting is that it's users who write the conversion explicitly, not that Sorry, I enumerated the options under the assumption that we are to provide auto-conversion. Obviously, we have an option not to provide. So, the complete list of options would be:
|
Exactly. I think 2.i is the right choice for types for which |
I see. Then I agree with you! |
@andy-thomason Are you Ok with this plan of not providing |
Yes. That sounds like a good plan. It is much more explicit. |
See #229 (comment)
Should hashmaps be converted to lists or environments by default? I think both conversions should be available, and then one can be made the default if the user doesn't specify what result they want.
There's also the question what happens if an unnamed list is converted to a hashmap. It should probably fail with an error.
The text was updated successfully, but these errors were encountered: