caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Fragile pattern matching?!
@ 2005-02-28 10:17 Alex Baretta
  2005-02-28 10:32 ` [Caml-list] " Keith Wansbrough
  2005-02-28 10:39 ` Luc Maranget
  0 siblings, 2 replies; 5+ messages in thread
From: Alex Baretta @ 2005-02-28 10:17 UTC (permalink / raw)
  To: Ocaml

We have an incomprehensibile warning when compiling code that looks like 
the following:

type value =
   | Int of int
   | Float of float
   | Int32 of int32
   | Int64 of int64
   | Bool of bool
   | String of string

let to_int value = match value with
   | Int x -> x
   | _ -> raise Some_exception

The compiler signals a warning for a fragile pattern matching at the "_" 
character.

Why in the world should this code signal such a warning?

Alex


-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Fragile pattern matching?!
  2005-02-28 10:17 Fragile pattern matching?! Alex Baretta
@ 2005-02-28 10:32 ` Keith Wansbrough
  2005-02-28 10:35   ` Alex Baretta
  2005-02-28 10:39 ` Luc Maranget
  1 sibling, 1 reply; 5+ messages in thread
From: Keith Wansbrough @ 2005-02-28 10:32 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Ocaml

Alex Baretta writes:

> let to_int value = match value with
>    | Int x -> x
>    | _ -> raise Some_exception
> 
> The compiler signals a warning for a fragile pattern matching at the "_" 
> character.
> 
> Why in the world should this code signal such a warning?

The reason is to do with code maintenance.  When in future you add
another constructor to the value type, the type checker will tell you
the locations of all matches that have now become incomplete, so that
you can fix them.  But the match in "to_int" above will never become
incomplete.  If the type checker passed this silently, then a bug
could easily be introduced.  Hence the warning.

HTH.

--KW 8-)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Fragile pattern matching?!
  2005-02-28 10:32 ` [Caml-list] " Keith Wansbrough
@ 2005-02-28 10:35   ` Alex Baretta
  0 siblings, 0 replies; 5+ messages in thread
From: Alex Baretta @ 2005-02-28 10:35 UTC (permalink / raw)
  To: Keith Wansbrough, Ocaml

Keith Wansbrough wrote:
> Alex Baretta writes:
> 
> 
>>let to_int value = match value with
>>   | Int x -> x
>>   | _ -> raise Some_exception
>>
>>The compiler signals a warning for a fragile pattern matching at the "_" 
>>character.
>>
>>Why in the world should this code signal such a warning?
> 
> 
> The reason is to do with code maintenance.  When in future you add
> another constructor to the value type, the type checker will tell you
> the locations of all matches that have now become incomplete, so that
> you can fix them.  But the match in "to_int" above will never become
> incomplete.  If the type checker passed this silently, then a bug
> could easily be introduced.  Hence the warning.

I understand the need for this kind of warning, but it seems to me that 
I have used the "match x with Something -> do_something x | _ -> 
raise_an_exception" for years. Suddenly, I see the compiler signal 
warnings where I would not expect to seen them.

What is the exact definition of fragile pattern matching?

Alex


-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Fragile pattern matching?!
  2005-02-28 10:17 Fragile pattern matching?! Alex Baretta
  2005-02-28 10:32 ` [Caml-list] " Keith Wansbrough
@ 2005-02-28 10:39 ` Luc Maranget
  2005-02-28 10:49   ` Alex Baretta
  1 sibling, 1 reply; 5+ messages in thread
From: Luc Maranget @ 2005-02-28 10:39 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Ocaml, Luc Maranget

> We have an incomprehensibile warning when compiling code that looks like 
> the following:
> 
> type value =
>   | Int of int
>   | Float of float
>   | Int32 of int32
>   | Int64 of int64
>   | Bool of bool
>   | String of string
> 
> let to_int value = match value with
>   | Int x -> x
>   | _ -> raise Some_exception
> 
> The compiler signals a warning for a fragile pattern matching at the "_" 
> character.
> 
> Why in the world should this code signal such a warning?
> 
> Alex
> 
> 
> -- 


Hello,

The warning (which you do not supply) attempt to be informative.

# ocamlc -w A alex.ml
File "alex.ml", line 13, characters 4-5:
Warning E: this pattern is fragile. It would hide
the addition of new constructors to the data types it matches.


This warning has been introduced in response to user demand. The idea is
to enforce some coding rule that promotes robustness. However the
coding rule is so strict that it was decided that standard users can ignore
it.

Of course if you specify -w A on the command line, then you get all warnings,
including the 'fragile pattern' warning.

As regards defaults for warnings, here is more or less what I get on
my ocaml installation.

% ocamlc -v 
The Objective Caml compiler, version 3.09+dev11 (2004-11-30)

% ocamlc -help
  ...
  -w <flags>  Enable or disable warnings according to <flags>:
     A/a enable/disable all warnings
     ...
     E/e enable/disable fragile match
     ...
     default setting is "Aelz"


-- Luc Maranget


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Fragile pattern matching?!
  2005-02-28 10:39 ` Luc Maranget
@ 2005-02-28 10:49   ` Alex Baretta
  0 siblings, 0 replies; 5+ messages in thread
From: Alex Baretta @ 2005-02-28 10:49 UTC (permalink / raw)
  To: Luc Maranget, Ocaml

Luc Maranget wrote:

> 
> Hello,
> 
> The warning (which you do not supply) attempt to be informative.
> 
> # ocamlc -w A alex.ml
> File "alex.ml", line 13, characters 4-5:
> Warning E: this pattern is fragile. It would hide
> the addition of new constructors to the data types it matches.

Got it! Makefile brain-damage on our part. We messed with the 
enabled/disable warnings and all of a sudden, without meaning to do it, 
we enable the E warning. This is why the warning, which used not to be 
flagged, suddenly began appearing.

Alex


-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-02-28 10:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-28 10:17 Fragile pattern matching?! Alex Baretta
2005-02-28 10:32 ` [Caml-list] " Keith Wansbrough
2005-02-28 10:35   ` Alex Baretta
2005-02-28 10:39 ` Luc Maranget
2005-02-28 10:49   ` Alex Baretta

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).