-
Notifications
You must be signed in to change notification settings - Fork 347
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
Parametric state, thought I could get this to type check but doesn't #148
Comments
@fxfactorial likely merlin does not show the type error while the error is there. I've seen that before. When a component is created, componentSpec should not contain type variables, so trying direct parametric state will give type errors. Using functors, perhaps on simplified version of the example, should give code that compiles. |
Some of the underlying reasons why state having type parameters is incompatible with React's model:
|
In your case the polymorphism is coming from OCaml's row polymorphic objects. Objects do also have a way to coerce away the polymorphic aspect using type repo = {. color: string};
type state = {repos: list(repo)};
type action =
| NewItem(repo);
type thing = {. "stars": int};
let s = ReasonReact.stringToElement;
let stars_component = ReasonReact.reducerComponent("Stars");
let make = children => {
...stars_component,
initialState: () => {repos: []},
reducer: (action, state) =>
switch action {
| NewItem(repo) => ReasonReact.Update({repos: [repo, ...state.repos]})
},
render: self => {
let inner =
self.state.repos
|> List.map(i => <p> (s(i#color)) </p>)
|> Array.of_list
|> ReasonReact.arrayToElement;
<div onClick=(self.reduce(repo => NewItem((repo :> repo))))>
(s("asdf"))
</div>;
}
}; |
Thanks! @cristianoc yes, I was thinking that using functor could delay creation, say create the module in a function. What is the reasoning behind the typing as it is? Jordan's intuitive React explanation makes sense, but wondering if there is also an type system explanation as well. @jordwalke Can props do this? |
For the sake of cleaning up the repo (and given how old this issue is) I'm going to close this out. Please re-open if this is still relevant. Thanks! |
Ping @cristianoc
I am trying to get this to type check but while it passes merlin, the compiler is not so happy and throws a type error
And I thought it might be because your own componentSpec definition of state doesn't not include a type variable but I thought I was getting around this with the local abstract type.
full code:
EDIT:
Also trying this and some variant of it but getting syntax errors on the constraint:
The text was updated successfully, but these errors were encountered: