English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Fragile pattern matching?!
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-02-28 (10:40)
From: luc.maranget@i...
Subject: Re: [Caml-list] Fragile pattern matching?!
> 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
> -- 


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

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