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

问题描述 投票:2回答:3

如何使编译器忽略某些向下转发模式匹配警告(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的任何警告错误。

.net f# functional-programming pattern-matching
3个回答
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 jobs的函数。


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

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
© www.soinside.com 2019 - 2024. All rights reserved.