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

Use binary ? operator as try-else #52

Open
dgkf opened this issue Sep 6, 2023 · 2 comments
Open

Use binary ? operator as try-else #52

dgkf opened this issue Sep 6, 2023 · 2 comments
Labels
meta-proposal Language proposals theme-internals Relates to internal operations of the language type-enhancement New feature or request

Comments

@dgkf
Copy link
Owner

dgkf commented Sep 6, 2023

R's error propagation is already quite powerful! All expressions are evaluated as though they can fail, and their errors are reported up through the call stack.

However, R's error capture and recovery is a bit messier. Instead of throwing errors, many codebases resort to returning NULL or some other value of significance to indicate an error, which can be interpreted as a special case by the calling function. This seems to be a pattern that has arisen out of the clunky error handling.

Perhaps we can make code both more readable and more stable by introducing better error handling features:

f <- function() {
  x <- g(x) ? g_default()
  paste0(x, "World")
}

The R world has no shortage of infix operators floating around, so deciding to reuse the binary infix behavior of a symbol should be done cautiously to make the best use of the syntax.

@dgkf dgkf added type-enhancement New feature or request theme-internals Relates to internal operations of the language meta-proposal Language proposals labels Sep 6, 2023
@sebffischer
Copy link
Collaborator

I quite like this proposal but I am wondering whether there is a way to make this more generic.
I think in R there are at least three type of objects that need to often be handled with special care, which are na, null and error.

Maybe one could add ?err ?na and ?null.

@sebffischer
Copy link
Collaborator

sebffischer commented Oct 10, 2024

Maybe one could even make this extendible, i.e. the language supports to add something like

?na = function(x) {
  is_na(x)
}

This is already implementable in R using %?na% but ?na just looks nicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta-proposal Language proposals theme-internals Relates to internal operations of the language type-enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants