English 中文(简体)
使用“最大变量识别符不应通常用于模式...”的语句是什么呢?
原标题:What s with "Uppercase variable identifiers should not generally be used in patterns..."?

此编译器类似 :

let test Xf Yf = Xf + Yf

此编译器不象 :

let test Xfd Yfd = Xfd + Yfd

Warning: Uppercase variable identifiers should not generally be used in patterns, and may indicate a misspelt pattern name.

也许我没有好好地谷歌, 但我一直没有找到 任何能解释为什么 功能参数就是如此的原因...

最佳回答

我同意这个错误信息看起来有点神秘, 但有一个很好的动机。 根据 F# 命名指南, 歧视工会的案例应该使用 < code> PascalCase 来命名, 而编译者正在试图确保您不会在模式匹配时意外错用一个案例的名称 。

例如,如果有下列结合:

type Side = 
  | Left 
  | Right

当参数为 Left 和“ wrong!” 时, 您可以写入以下功能, 打印“ ok ” 。 否则 :

let foo a = 
  match a with 
  | Lef -> printfn "ok"
  | _ -> printfn "wrong!"

代码中有一个打字符 - 我写了 < code> Lef - 但代码仍然有效, 因为 < code> Lef 可以被解释为一个新的变量, 因此匹配的侧面会指派给 < code> Lef , 并且总是运行第一个大小写。 关于大写识别符的警告有助于避免这种情况 。

问题回答

F## 试图针对活动模式执行案件规则,

let f X = 
    match X with
    |X -> 1
    |_ -> 2

这是相当混乱的。 另外, 函数参数与模式相似, 您可以做

let f (a,b,_) = a,b

例如,不清楚为什么第三字母触发警告





相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...

热门标签