Replies: 5 comments 1 reply
-
You could try to use some monadic functions ( pub fn Add::from_json(value : @json.JsonValue) -> Add? {
value
.as_object()
.bind(
fn(value) {
let a = value.get("a").map(Json::as_number).flatten()
let b = value.get("b").map(Json::as_number).flatten()
match (a, b) {
(Some(a), Some(b)) => Some({ a: a.to_int(), b: b.to_int() })
_ => None
}
},
)
} |
Beta Was this translation helpful? Give feedback.
-
Sorry, but that seems to me to be even harder to read than my current workaround. |
Beta Was this translation helpful? Give feedback.
-
Why not use json pattern?
|
Beta Was this translation helpful? Give feedback.
-
Thank you, @Yoorkin , I will in this case. But I would still like this feature request to be considered because there are myriad opportunities to take advantage of this in non-JSON-related code. ...Or simply bring back the |
Beta Was this translation helpful? Give feedback.
-
hi @gmlewis we will add features like this which is more general and readable:
The |
Beta Was this translation helpful? Give feedback.
-
It seems like this README.md is out-of-date:
core/option/README.md
Lines 45 to 72 in 34096f4
It says:
AND YET... THIS WAS ONE OF MY FAVORITE MoonBit FEATURES that was eliminating so much ugly boilerplate code like this:
https://github.com/gmlewis/moonbit-pdk/blob/40948f7d08128b4c36036276d9489f32be8e99db/examples/add/add.mbt#L7-L19
which reminds me of Java in a really bad way.
So I would like to make a feature request for
Option::or_return_none()
which would allow that same code to be written like this:Unfortunately, this feature request may require compiler support, without which it might not be possible to write.
Beta Was this translation helpful? Give feedback.
All reactions