-
Notifications
You must be signed in to change notification settings - Fork 428
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
Safe call operators ?. #2142
Safe call operators ?. #2142
Conversation
I love this feature. I have a lot of nested switch statement like this when working with graphql. |
This is specific to |
I like the idea of optional-access operators, but I'm not super happy about the syntax :/ feels pretty obfuscating, hard to google, etc. What if we instead extended
Which, though a little less terse, I think is more understandable / googleable / cmd+clickable for newcomers. Also, in the context of the
it's all the same function you're calling. |
Most Javascript developers know Typescripts I think the problem for newcomers are the bind operators |
But the idea of promoting attribute access at this syntax level is a very OO kind of a thing (which typescript, js, kotlin, and swift are), and all of those implementations require some amount of type-loose-shanigans. |
Note that you can do let name = switch response {
| None => None
| Some({user: {name}}) => Some(name)
}; when it's a record, but not when it's a JS object. That's the original issue. |
So with a javascript object we could similarly do open Option; // assuming it gives us map, bind
let userName = response->bind(_##user)->bind(_##name)
// or, for if you want more clarity/verbosity
let userName = response->bind(response => response##user)->bind(user => user##name) I think this gets what we need (freedom from endless switch statements) without adding complexity to the language |
@jaredly this is very similar to Scala's placeholder syntax which I miss very much |
@jaredly after a quick google, seems like Rust has this operator as well? see https://doc.rust-lang.org/book/second-edition/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator |
@Gregoirevda feels a little weird to have differentiate between bind and map, can the parser not detect and write correct statement according to the type? |
@cloudkite Rust has an operator for propagating errors - a
definitely not, unfortunately -- the parser has no knowledge of types -- the type inference phase runs well after refmt is done doing its work. |
@jaredly coming from Standard ML, OCaml has, among others, OO paradigm: Object, Classes and inheritence. Rust, even if coming from Standard ML, isn't OO. So, saying that About Kotlin and Swift, let s hope reason-react-native will be a success. Android and IOS devs will probably appreciate the Even better than cmd+clickable, with 3 lines of code comment to explain what the function is doing, is good naming for functions. @cloudkite unfortunately, the parsers just gets strings and tokens. |
Here's an experiment for auto-unwrapping options when accessing fields: cristianoc/ocaml#1 |
😮🤯👏👏👏 that's veryyy cool |
Feels a bit misleading to auto-unwrap 🤔 what's the difficulty in learning about let username = response >>= owner >>| name <|> "no_username_available";
"Hello " ++ username Defining custom operators like above is already completely supported, has plenty of literature around it, has no significant impact on the syntax of the language, and can be even optimized away by the compiler (like Flambda does). This is starting to smell a lot like the |
Ideally, we should have record typing for Javascript objects. I'll leave this open until some proposition is made in bsb |
I made a ppx to try out this safe-call kind of thing (to relieve the pain of working with graphql results) in userland https://github.com/jaredly/get_in |
Cool you made a ppx for it! |
The idea being that we can prove out the concept for a while, and then have a better idea of how much it is needed, etc. to decide whether to add it to the core syntax :) |
Seasoned OCaml developers avoid the OO features of OCaml, some of them even wish they can remove it from the language. It was added mostly as a marketing ploy to compete with Java. |
This PR adds support for
?.
,?#
,?.+
and?#+
Useful for accessing deeply nested properties.
Instead of writing
You can write
Same goes for accessing Javascript objects with
?#
and?#+
.Note the
+
for record and Javascript objects access operators representing a bind.?.
represents a map:?.+
represents a bindIf the field is optional, use
?.
otherwise use?.+
Note on token associativity:
You would think
?.
is left associative(a?.b)?.c
, but since?.
is not runtime execution but sugar for a switch, it has to be right associative to construct itself from the deeply nested part to its parent.Right associativity: (current)
left associativity
TODO:
+
is appropriate)reason_pprint_ast
Linked issue: #1928