我想了解 example server code 它使用 WebSocket 图书馆。
我发现了一个奇怪的案例表达,我无法理解:
case msg of _ | not (prefix `T.isPrefixOf` msg) -> WS.sendTextData conn ("Wrong announcement" :: Text) | any ($ fst client) [T.null, T.any isPunctuation, T.any isSpace] -> WS.sendTextData conn ("Name cannot " `mappend` "contain punctuation or whitespace, and " `mappend` "cannot be empty" :: Text) | clientExists client clients -> WS.sendTextData conn ("User already exists" :: Text) | otherwise -> flip finally disconnect $ do -- ...
这张外卡是什么意思?case表达式的语法如下:
case expression of pattern -> result pattern -> result pattern -> result ...
为什么是 _ 有必要,为什么作者可以在里面使用防护装置?
_
case irrelevant of _ | condition1 -> e1 | condition2 -> e2 ... | otherwise -> eO
是写一连串 if then else .
if then else
if condition1 then e1 else if condition2 then e2 ... else eO
这个 irrelevant 表达是无关的。它的值与 _ ,它总是成功并丢弃值。
irrelevant
你的代码使用混乱 case msg of ... 但是 msg 被忽略。通常一个人写 case () of ... 强调它的价值是不重要的。
case msg of ...
msg
case () of ...