Thank you.
JS solution: children = Array.isArray(children) ? children : [children]
Thank you.
JS solution: children = Array.isArray(children) ? children : [children]
I think the complexity is separation-of-concerns. I’m composing multiple different types of documents, but they are all mongo documents.
Gotta ask yourself, is the complexity from separaing these concerns actually gaining you any value in the long run? Are there often times where you want a user that doesn’t have an ID?
A way around it is to treat Document.t as a box with an ID that contains a value, and then use an identy casting function to “extract” the data
module Document = {
type t<'a> = {id: string}
external data: t<'a> => 'a = "%identity"
}
let userDocument: Document.t<user> = {/**/}
let id = userDocument.id
let user = Document.data(userDocument)
children = Array.isArray(children) ? children : [children]
hmm I can’t think of a use case where modifying the react children would be useful except for intentionally calling Array.map
on them to do something, which that I would consider an anti pattern. You can probably implement something similar using context
No, the complexity definitely isn’t worth it, I’m just trying to learn how to do things properly in ReScript. Thank you so much for your reply, that was a really neat way of doing it.
hmm I can’t think of a use case where modifying the react children would be useful except for intentionally calling
Array.map
on them to do something, which that I would consider an anti pattern. You can probably implement something similar using context
So, the thing is, if your React component receives a single child, children
is not an array. If you receive multiple children, children
is an array.
Therefore, if you’re looking to do some form of map operation on the children, you first have to make sure it’s an array. That’s where my snippet comes into play.
There’s React.Children
, module with map
, forEach
and other stuff.
Link to the bindings: https://github.com/rescript-lang/rescript-react/blob/0f74265112bc8b725d361634fd49392a0ae29707/src/React.res#L56
You can do it only with %raw
Why would you need to do a instanceof Error
? Perhaps there is a more idiomatic way of doing what you are trying to accomplish.
To add to this, note that this isn’t just a ReScript thing. In ReactJS you should treat children
as opaque and use the React.Children
functions.
Thank you, this clarified it for me.
The cors API returns an Error if something fails, otherwise some success object:
type corsFn = (Next.Request.t, Next.Response.t, t => unit) => unit
@module("cors")
external create: {..} => corsFn = "default"
let allow = (corsFn: t, req: Next.Request.t, res: Next.Response.t) =>
Promise.make((resolve, _) =>
corsFn(req, res, result => {
//if (result instanceof Error) {
//reject(result)
//return
//}
resolve(. result)
})
)
Which cors API is this? Can you point to some documentation or post a JavaScript example which shows the behaviour?
Thank you @yawaramin. I appreciate your effort to help but we’re focusing too much on the specifics. Imagine that such an API exists - how do I tackle it with ReScript? After all, it’s ReScript that I need help wrapping my head around, not some bad API specification!
Just asking because I’ve never come across any API that actually returns an Error
object on failure (instead of throwing an exception or returning null
or calling an onError
handler).
Actually it’s a good pattern in typescript, because this way you can describe possible errors of an API with typesystem. It’s kind of an alternative of result type.
But don’t forget difference between error and exception flow, when use something like this.
For this case, I’d either create a function to test instnanceof Error, or a function to convert it to ReScript’s result type. But you need to use %raw for the instanceof anyway.
So basically (in TypeScript terms) type Result<A> = A | Error
? OK, I get it. Imho not a great approach (what happens when A
is supposed to be Error
) but can of course be handled with %raw
.
No, in this approach you directly type data or a function return value without creating result type. In this case the code using this approach looks very similar to go, where after every function call you have an if statements, handling possible errors.
To be able to map over result in typescript people usually use a third-party library with either-monad. I personally used sweet-monads/either at master · JSMonk/sweet-monads · GitHub
I’ve never seen such a problem using this approach.