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

Get SymPair to return Option<Symbol> #225

Conversation

yutannihilation
Copy link
Contributor

Fix #195

This pull request is to remove the necessity to use unbound value to represent the absence of the name. I decided to follow Andy's comment. Instead, users can use "" (empty string).

This is consistent with the behavior of R (c.f. #195 (comment), #195 (comment))

While this is a breaking change in that SymPair::sym_pair() now returns (Option<Robj>, Robj) instead of (Robj, Robj), I expect this doesn't affect the existing users much as this is rather an expert-only feature at the moment.

Comment on lines +68 to +70
if let Some(n) = n {
unsafe { Rf_defineVar(n.get(), v.get(), robj.get()) }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing not very obvious is what we should do when the user try to create an environment from a pairlist containing unnamed element. Currently it's just ignored. Should we raise an error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is ignored? The unnamed element only, or the entire call? And if you triggered an error, would it be compile time or runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unnamed element only, or the entire call?

Currently this ignores the unnamed elements only.

And if you triggered an error, would it be compile time or runtime?

Runtime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear to me. Usually, my philosophy is that code should use meaningful fallback behavior on empty input, so that would argue against raising an error. I have encountered so many cases where code would complain about NA or NULL input instead of silently ignoring it, and then I have to special-case these situations, and the code I'm calling is also special-casing, and it just doesn't make sense.

I assume the counter-argument is that silently ignoring invalid input can create difficult-to-find bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting inclined to raise an error, rather than handling the invalid inputs gracefully. I think this operation is for special cases when there's a lot of inputs generated programmatically, as the example shows. For more dynamic inputs, users can process the elements one by one with Environment::set_local() by themselves.

    ///     let names_and_values = (0..100).map(|i| (format!("n{}", i), i));
    ///     let mut env = Environment::from_pairs(global_env(), names_and_values);
    ///     assert_eq!(env.len(), 100);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sounds good to me.

@clauswilke
Copy link
Member

I have reviewed this but don't feel qualified to assess whether the change from (Robj, Robj) to (Option<Robj>, Robj) will cause any issues. @andy-thomason Could you review also?

Copy link
Contributor

@andy-thomason andy-thomason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a bit faster if anything.

It is also a bit clearer when names are not present.

@andy-thomason andy-thomason merged commit be761f3 into extendr:master Jul 19, 2021
@yutannihilation yutannihilation deleted the fix/issue-195-sympair-return-option branch July 22, 2021 03:02
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.

Does unbound_value need to be exposed just to support pairlist creation from string tuples?
3 participants