rescript

How to send the result of a pipline into a switch statement?


I want to send the result of a pipeline into a switch statement

let onWithOptions = (~key: string, eventName, options: options, decoder) =>
  onCB(eventName, key, event => {
    
  ...
    event |> Tea_json.Decoder.decodeEvent(decoder) |> 
      switch x {
      | Ok(a) => Some(a)
      | Error(_) => None
      }

  })

I have tried to do it this way but it didn't work x is not recognizable in the switch statement

let onWithOptions = (~key: string, eventName, options: options, decoder) =>
  onCB(eventName, key, event => {
    
  ...
    let x = event |> Tea_json.Decoder.decodeEvent(decoder) |> 
      switch x {
      | Ok(a) => Some(a)
      | Error(_) => None
      }

  })

Any idea on how this could be done?


Solution

  • You could create an anonymous function to pass it into:

        let x = event |> Tea_json.Decoder.decodeEvent(decoder) |> (x =>
          switch x {
          | Ok(a) => Some(a)
          | Error(_) => None
          })
    

    This is the closest equivalent in Rescript to the function shot-hand in OCaml. But in this scenario is really just a convoluted way of giving the value a name:

        let x = event |> Tea_json.Decoder.decodeEvent(decoder)
        let x =
          switch x {
          | Ok(a) => Some(a)
          | Error(_) => None
          }