如何使编译器忽略某些向下转发模式匹配警告(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
的任何警告错误。
答案 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
虽然第二个在类型检查时被拒绝,因为student
和teacher
不相等:
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