User-provided type inside a module


#1

Hey,

I am working on a module that depends on user-defined type. I am not sure if there’s a better way than the below approach:

module type NavigationConfig = {type route;};

module CreateNavigation = (Config: NavigationConfig) => {
  module StackNavigator = {
    type action =
      | Push(Config.route)
      | Pop;
    type state = {routes: list(Config.route)};
    let component = ReasonReact.reducerComponent("CallstackRerouteRouter");
    let make = (~initialRoute, children) => {
      ...component,
      initialState: () => {routes: [initialRoute]},
      reducer: (action, state) =>
        switch action {
        | Push(route) => ReasonReact.Update({routes: [route, ...state.routes]})
        | _ => ReasonReact.NoUpdate
        },
      render: self =>
        self.state.routes
        |> List.map(route => children(~currentRoute=route))
        |> Array.of_list
        |> ReasonReact.arrayToElement
    };
  };
};

I don’t like the need of passing type route around - feels like it should be easy to let compiler infer it. Unfortunately, I was unable to do so in any of my prior attempts.

Anyone?


#2

Your current approach looks like idiomatic ReasonML to me. (and a correct way for working with functor).

I’m not seeing where do you have to do it in the snippet


#3

Thanks for your feedback. I was wondering whether there was better way than using a functor if the only part of Config was just route.

I’ll probably add more elements to Config in a bit, so I guess that’s okay.


#4

This is definitely a valid use case for a functor.