-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a document about optional arg (#247)
- Loading branch information
1 parent
2f4ae22
commit ebccac9
Showing
4 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Optional Argument | ||
|
||
To represent an optional argument, you can wrap it with `Option`. Then, the | ||
corresponding R function sets the default value of `NULL` on the argument. | ||
|
||
``` rust | ||
#[savvy] | ||
fn default_value_vec(x: Option<IntegerSexp>) -> savvy::Result<Sexp> { | ||
if let Some(x) = x { | ||
x.iter().sum::<i32>().try_into() | ||
} else { | ||
(-1).try_into() | ||
} | ||
} | ||
``` | ||
|
||
``` r | ||
function(x = NULL) { | ||
.Call(savvy_default_value_vec__impl, x) | ||
} | ||
``` | ||
|
||
This function works with or without the argument. | ||
|
||
``` r | ||
default_value_vec(1:10) | ||
#> [1] 55 | ||
|
||
default_value_vec() | ||
#> [1] -1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Handling Salars | ||
# Handling Scalar | ||
|
||
## Input | ||
|
||
|