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

NA handling #27

Open
JosiahParry opened this issue Feb 26, 2024 · 4 comments
Open

NA handling #27

JosiahParry opened this issue Feb 26, 2024 · 4 comments

Comments

@JosiahParry
Copy link
Contributor

JosiahParry commented Feb 26, 2024

Use extendr types for inputs which allow for missing values. Illustrate using match or ifelse statements to handle missing values. Discuss options:

  • return a missing value (Rscalar::na() method or ().into_robj() for null
  • Return a default value
  • skip and return a smaller result using filter_map()
@Ilia-Kosenkov
Copy link
Member

We also have Nullable<T> to handle explicit NULL values

@JosiahParry
Copy link
Contributor Author

@JosiahParry
Copy link
Contributor Author

Here's a minimal example of using this. @Ilia-Kosenkov what would you say the desired use pattern is for Nullable?

I can see it being helpful in my code here: https://github.com/JosiahParry/rsgeo/blob/32d0d7e2f0faab349b5261f964a0091760b55fcd/src/rust/src/boundary.rs#L149-L159

struct MyStruct();

#[extendr]
impl MyStruct {
}

#[extendr]
fn mynull(x: bool) -> Nullable<MyStruct> {
    match x {
        true => Nullable::NotNull(MyStruct()),
        false => Nullable::Null,
    }
}
mynull(TRUE)
#> <pointer: 0x1>
#>   attr(,"class")
#> [1] "MyStruct"
mynull(FALSE)
#> NULL

@Ilia-Kosenkov
Copy link
Member

I think one of the best use-cases is NULL default values, e.g.

#[extendr(use_try_from = true)]
fn with_optional_input(#[default = "NULL"] idx : Nullable<Integers>) -> Integers {
    match idx {
        NotNull(x) => x,
        Null => (0..5).collect::<Integers>() /* do not remember the syntax */
    }
}

@CGMossa CGMossa transferred this issue from extendr/user-guide-old Jun 30, 2024
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

No branches or pull requests

2 participants