0

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?

glennsl
  • 28,186
  • 12
  • 57
  • 75
Fer iel
  • 23
  • 4

1 Answers1

0

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
      }
glennsl
  • 28,186
  • 12
  • 57
  • 75