Compile error using fast pipe operator after pipe last in ReasonML

折月煮酒 提交于 2019-12-13 13:41:08

问题


The way that the "fast pipe" operator is compared to the "pipe last" in many places implies that they are drop-in replacements for each other. Want to send a value in as the last parameter to a function? Use pipe last (|>). Want to send it as the first parameter? Use fast pipe (once upon a time |., now deprecated in favour of ->).

So you'd be forgiven for thinking, as I did until earlier today, that the following code would get you the first match out of the regular expression match:

Js.String.match([%re "/(\\w+:)*(\\w+)/i"], "int:id")
|> Belt.Option.getExn
-> Array.get(1)

But you'd be wrong (again, as I was earlier today...)

Instead, the compiler emits the following warning:

We've found a bug for you!
OCaml preview 3:10-27
This has type:
  'a option -> 'a
But somewhere wanted:
  'b array

See this sandbox. What gives?


回答1:


Looks like they screwed up the precedence of -> so that it's actually interpreted as

Js.String.match([%re "/(\\w+:)*(\\w+)/i"], "int:id")
|> (Belt.Option.getExn->Array.get(1));

With the operators inlined:

Array.get(Belt.Option.getExn, 1, Js.String.match([%re "/(\\w+:)*(\\w+)/i"], "int:id"));

or with the partial application more explicit, since Reason's syntax is a bit confusing with regards to currying:

let f = Array.get(Belt.Option.getExn, 1);
f(Js.String.match([%re "/(\\w+:)*(\\w+)/i"], "int:id"));

Replacing -> with |. works. As does replacing the |> with |..

I'd consider this a bug in Reason, but would in any case advise against using "fast pipe" at all since it invites a lot of confusion for very little benefit.




回答2:


Also see this discussion on Github, which contains various workarounds. Leaving @glennsl's as the accepted answer because it describes the nature of the problem.

Update: there is also an article on Medium that goes into a lot of depth about the pros and cons of "data first" and "data last" specifically as it applies to Ocaml / Reason with Bucklescript.



来源:https://stackoverflow.com/questions/54957566/compile-error-using-fast-pipe-operator-after-pipe-last-in-reasonml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!