Force compiler warning if branch possible?


#1

Is it possible to get the compiler to throw an error or a warning if a certain branch of my path becomes possible?

For example I have this code right now:

  switch (route) {
  | r when r->in_(publicOnlyRoutes) => PublicOnly
  | r when r->in_(publicRoutes) => Any
  | r when r->in_(authenticatedRoutes) => LoginRequired
  | _ => Js.log("SHOULD NOT BE POSSIBLE, THROW COMPILER ERROR")
  };

publicOnlyRoutes, publicRoutes, authenticatedRoutes are all lists with routes in them. Which means in my switch statement it should only be possible to get to the default case if I forgot to add a route to one of the lists. If this happens it would be nice if the compiler could warn me.

EDIT:

I think I found a workaround

  switch (route) {
  | Route.Home => Any
  | r when r->in_(publicOnlyRoutes) => PublicOnly
  | r when r->in_(publicRoutes) => Any
  | r when r->in_(authenticatedRoutes) => LoginRequired
  };

EDIT 2:

Finally settled on the simplest option:

let routeType = (route: Route.t): routeType =>
  Route.(
    switch (route) {
    | Home
    | NotFound
    | Loading => Any

    | Login
    | Register => PublicOnly

    | Admin => LoginRequired
    }
  );