OCaml - 谁能解释一下 "pattern-matching is not exhaustive"
OCaml - can someone explain this "pattern-matching is not exhaustive"
所以我有这段代码:
let rec minBound s =
match s with
Rect (s,i)-> Rect(s,i)
|Circle (c,r)-> getRectOutCircle (c,r)
|Union(l,r)->
let Rect(sl, il) = (minBound l) in
let Rect(sr, ir) = (minBound r) in
let (xsl,ysl) = sl in
let (xil,yil) = il in
let (xsr,ysr) = sr in
let (xir,yir) = ir in
Rect( ((min xsl xsr), (min ysl ysr)) ,
(( max xil xir), (max yil yir)) )
|Intersection(l,r)->
let Rect(sl, il) = (minBound l) in
let Rect(sr, ir) = (minBound r) in
let (xsl,ysl) = sl in
let (xil,yil) = il in
let (xsr,ysr) = sr in
let (xir,yir) = ir in
Rect( ((min xsl xsr), (min ysl ysr)) ,
(( max xil xir), (max yil yir)) )
|Subtraction(l,r) ->
let Rect(sl, il) = (minBound l) in
let Rect(sr, ir) = (minBound r) in
let (xsl,ysl) = sl in
let (xil,yil) = il in
let (xsr,ysr) = sr in
let (xir,yir) = ir in
Rect( ((min xsl xsr), (min ysl ysr)) ,
(( max xil xir), (max yil yir)) )
;;
谁能解释一下为什么会出现以下警告?
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(Circle (_, _)|Union (_, _)|Intersection (_, _)|Subtraction (_, _))val minBound : shape -> shape = <fun>
这里有更多信息可以提供帮助!!
type point = float*float;;
type shape = Rect of point*point
| Circle of point*float
| Union of shape*shape
| Intersection of shape*shape
| Subtraction of shape*shape
;;
您的函数 minBound
return 类型 shape
。但是你的递归调用假设它只能 return 一种形状。编译器警告您它可能 return 其他类型的形状。
这是一个常见问题。基本上你知道 minBound
总是 return 是一个 Rect
,但编译器不知道。
一个可能的解决方案是 minBound
return point * point
.
所以我有这段代码:
let rec minBound s =
match s with
Rect (s,i)-> Rect(s,i)
|Circle (c,r)-> getRectOutCircle (c,r)
|Union(l,r)->
let Rect(sl, il) = (minBound l) in
let Rect(sr, ir) = (minBound r) in
let (xsl,ysl) = sl in
let (xil,yil) = il in
let (xsr,ysr) = sr in
let (xir,yir) = ir in
Rect( ((min xsl xsr), (min ysl ysr)) ,
(( max xil xir), (max yil yir)) )
|Intersection(l,r)->
let Rect(sl, il) = (minBound l) in
let Rect(sr, ir) = (minBound r) in
let (xsl,ysl) = sl in
let (xil,yil) = il in
let (xsr,ysr) = sr in
let (xir,yir) = ir in
Rect( ((min xsl xsr), (min ysl ysr)) ,
(( max xil xir), (max yil yir)) )
|Subtraction(l,r) ->
let Rect(sl, il) = (minBound l) in
let Rect(sr, ir) = (minBound r) in
let (xsl,ysl) = sl in
let (xil,yil) = il in
let (xsr,ysr) = sr in
let (xir,yir) = ir in
Rect( ((min xsl xsr), (min ysl ysr)) ,
(( max xil xir), (max yil yir)) )
;;
谁能解释一下为什么会出现以下警告?
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(Circle (_, _)|Union (_, _)|Intersection (_, _)|Subtraction (_, _))val minBound : shape -> shape = <fun>
这里有更多信息可以提供帮助!!
type point = float*float;;
type shape = Rect of point*point
| Circle of point*float
| Union of shape*shape
| Intersection of shape*shape
| Subtraction of shape*shape
;;
您的函数 minBound
return 类型 shape
。但是你的递归调用假设它只能 return 一种形状。编译器警告您它可能 return 其他类型的形状。
这是一个常见问题。基本上你知道 minBound
总是 return 是一个 Rect
,但编译器不知道。
一个可能的解决方案是 minBound
return point * point
.