OCaml - может кто-нибудь объяснить это "сопоставление с образцом не является исчерпывающим" - PullRequest
0 голосов
/ 02 апреля 2020

Итак, у меня есть этот кусок кода:

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
;;

1 Ответ

1 голос
/ 02 апреля 2020

Ваша функция minBound возвращает тип shape. Но ваши рекурсивные вызовы предполагают, что он может вернуть только один вид фигуры. Компилятор предупреждает вас, что он может возвращать фигуры других типов.

Это распространенная проблема. В основном вы знаете, что minBound всегда возвращает Rect, но компилятор не знает его.

Одним из возможных решений было бы иметь minBound return point * point.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...