在ReasonML中,在最后一个管道之后使用快速管道运算符编译错误 [英] Compile error using fast pipe operator after pipe last in ReasonML

查看:118
本文介绍了在ReasonML中,在最后一个管道之后使用快速管道运算符编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多 地方表示它们是彼此的直接替代。是否要将值作为最后一个参数发送给函数?最后使用管道( |> )。要将其作为第一个参数发送?使用快速管道(曾经在 |。上使用,现在已弃用了-> )。

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?

推荐答案

看起来像是搞砸了-> ,因此它实际上被解释为

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));

内联运算符:

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

或部分应用程序更明确,因为Reason的语法在计算时有些混乱:

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"));

用<$ c替换-> $ c> |。有效。与将 |> 替换为 |。一样。

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

我认为这是Reason中的错误,但是无论如何都建议不要使用快速管道,因为它会带来很多混乱,而带来的好处却很小。

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.

这篇关于在ReasonML中,在最后一个管道之后使用快速管道运算符编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆