问题描述
最近我遇到了以下代码,这让我很困扰
lowerSafeForeignCall dflags块|(entry,middle,CmmForeignCall {..})<-blockSplit块=做-阻止东西-Block不会以安全的外线通话结尾:|否则=返回块
这段代码来自 https://phabricator.haskell.org/rGHCb0534f78a73f972e279eed4447a5687bd6a8308e在文件editor/cmm/CmmLayoutStack.hs中
第983行
我真的很想知道第二行中的<-是什么.我相信 lowerSafeForeignCall 是一个函数,并且 | 和'否则'表示此函数使用了防护功能.所以
(entry,middle,CmmForeignCall {..})<-blockSplit块
必须是布尔类型.但是<-在任何do块之外.我在网上做了一些搜索,但仍然没有关于此用法的任何线索.
这是图案后卫:
[...]
在常规防护只限于布尔检查的情况下,模式防护可以与任意模式匹配并定义局部变量.(在您的情况下, entry
, middle
和 CmmForeignCall
的内容将直接在函数主体中提供.)
您可以认为布尔值守卫等同于模式为 True
的模式守卫:
|expr
类似
|真<-expr
I came across with the following code recently and it bothers me a lot
lowerSafeForeignCall dflags block
| (entry, middle, CmmForeignCall { .. }) <- blockSplit block
= do
-- do block stuffs
-- Block doesn't end in a safe foreign call:
| otherwise = return block
This piece of code is fromhttps://phabricator.haskell.org/rGHCb0534f78a73f972e279eed4447a5687bd6a8308e
in file compiler/cmm/CmmLayoutStack.hs
line 983
I really would like to konw what is this <- in the second line.I believe lowerSafeForeignCall is a function and the | and 'otherwise' indicate this function uses guards. So
(entry, middle, CmmForeignCall { .. }) <- blockSplit block
must be of type Bool. But the <- is outside any do block.I did some search online but still not a single clue about this usage.
That's a pattern guard:
[...]
Where normal guards are limited to a boolean check, pattern guards can match against an arbitrary pattern and define local variables. (In your case entry
, middle
, and the contents of CmmForeignCall
will be directly available in the function body.)
You can think of boolean guards as equivalent to pattern guards with a pattern of True
:
| expr
works like
| True <- expr
这篇关于在do块外,左箭头<-是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!