没有警告错误的堕落工会案件

时间:2017-12-29 09:09:08

标签: .net f# functional-programming pattern-matching

如何使编译器忽略某些向下转发模式匹配警告(FS0025),但必须在同一文件中捕获其他FS0025警告?

例如,第一个模式匹配(Student studentName) = john永远不会抛出错误,所以我希望编译器删除不必要的警告。

type Job = Student of string | Teacher of string

let john = Student "John"

(* FS0025: Incomplete pattern matches on this expression. For example,
the value 'Teacher _' may indicate a case not covered by the pattern(s). *)
let (Student studentName) = john
let (Teacher teacherName) = john // runtime error

我试过了:

#nowarn "25"
let (Student studentName) = john
#warn "25"
let (Teacher teacherName) = john

但它没有显示let (Teacher teacherName) = john的任何警告错误。

3 个答案:

答案 0 :(得分:2)

如果您愿意使用GADT在类型中记录使用的构造函数,您可以编写类似这样的东西(OCaml语法,我没有安装F#):

type student
type teacher

type _ job =
  | Student : string -> student job
  | Teacher : string -> teacher job

let john = Student "John"

然后接受您的第一个模式匹配而不发出警告(总计:类型student job的值只有一个构造函数):

let (Student studentName) = john

虽然第二个在类型检查时被拒绝,因为studentteacher不相等:

let (Teacher teacherName) = john

您可以通过编写job中的函数来编写范围超过所有'a job的函数。

答案 1 :(得分:1)

在这个特定示例中,两个案例都具有相同的数据,一个名称字符串,因此您可以模式匹配以获取名称:

let name = match john with Student n | Teacher n -> n

如果您要多次这样做,可以将其添加为该类型的成员:

type Job =
    | Student of string
    | Teacher of string
    member this.Name = match this with Student n | Teacher n -> n

let john = Student "John"
let name = john.Name

答案 2 :(得分:1)

如果无法访问GADT,您必须以附加的包装类型的形式添加一些额外的样板:

type Student = Student of string 
type Teacher = Teacher of string

type Job = StudentJob of Student | TeacherJob of Teacher

let john = Student "John"

let (Student studentName) = john // no warning
let (Teacher teacherName) = john // compile error