caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
@ 2016-04-27  9:22 William
  2016-04-27  9:28 ` David Allsopp
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: William @ 2016-04-27  9:22 UTC (permalink / raw)
  To: caml-list

Hello,

with ocaml 4.03, I came with many warnings such as this one :

let lst = [] in
try
    let fst = List.hd lst in
    Printf.printf "fst:%s\n" fst
with
| Failure "hd" -> Printf.printf "empty list\n";;

Characters 123-127:
| Failure "hd" -> Printf.printf "empty list\n";;
            ^^^^
Warning 52: the argument of this constructor should not be matched against a
constant pattern; the actual value of the argument could change
in the future.



It occurs typically for Failure "hd", Failure "tl", Failure 
"float_of_string", Failure "int_of_string", Failure "lexing: empty 
token", Invalid_argument "Filename.chop_extension", ...

How are we supposed to handle this nicely with the standard library ? Is 
there a better approach to catch those exceptions ?


Best regards,

William

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

* RE: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27  9:22 [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern William
@ 2016-04-27  9:28 ` David Allsopp
  2016-04-27  9:36 ` Daniel Bünzli
  2016-04-27 11:01 ` Gabriel Scherer
  2 siblings, 0 replies; 11+ messages in thread
From: David Allsopp @ 2016-04-27  9:28 UTC (permalink / raw)
  To: William, caml-list

William wrote:
> Hello,
> 
> with ocaml 4.03, I came with many warnings such as this one :
> 
> let lst = [] in
> try
>     let fst = List.hd lst in
>     Printf.printf "fst:%s\n" fst
> with
> | Failure "hd" -> Printf.printf "empty list\n";;
> 
> Characters 123-127:
> | Failure "hd" -> Printf.printf "empty list\n";;
>             ^^^^
> Warning 52: the argument of this constructor should not be matched against
> a constant pattern; the actual value of the argument could change in the
> future.
> 
> 
> 
> It occurs typically for Failure "hd", Failure "tl", Failure
> "float_of_string", Failure "int_of_string", Failure "lexing: empty token",
> Invalid_argument "Filename.chop_extension", ...
> 
> How are we supposed to handle this nicely with the standard library ? Is
> there a better approach to catch those exceptions ?

In most (hopefully all) of those instances, what you're matching is Failure - i.e. the function in question only raises it in one instance, so I think the idea now is that you should match against [Failure _] rather than including the string in the match. failwith is used in too many places in the Standard Library, but that of course can't now be changed (cf. ExtList.List.Empty_list and others)

That said, Failure "lexing: empty token" is an odd one, because I certainly have lexers which have to emulate that...


David

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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27  9:22 [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern William
  2016-04-27  9:28 ` David Allsopp
@ 2016-04-27  9:36 ` Daniel Bünzli
  2016-04-27  9:43   ` Jacques-Henri Jourdan
  2016-04-27 11:01 ` Gabriel Scherer
  2 siblings, 1 reply; 11+ messages in thread
From: Daniel Bünzli @ 2016-04-27  9:36 UTC (permalink / raw)
  To: William; +Cc: caml-list

Le mercredi, 27 avril 2016 à 11:22, William a écrit :
> How are we supposed to handle this nicely with the standard library ? Is
> there a better approach to catch those exceptions ?

Capture them immediately around the function call that raises with a _ pattern.  
For example:  

match (try Some (List.hd lst) with Failure _ -> None) with
| None -> Printf.printf "empty list\n"
| Some hd -> …

That's the way these functions should have been defined in the first place.

Best,  

Daniel



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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27  9:36 ` Daniel Bünzli
@ 2016-04-27  9:43   ` Jacques-Henri Jourdan
  2016-04-27  9:51     ` Roberto Di Cosmo
  2016-04-27 10:00     ` Daniel Bünzli
  0 siblings, 2 replies; 11+ messages in thread
From: Jacques-Henri Jourdan @ 2016-04-27  9:43 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 746 bytes --]



On 04/27/2016 11:36 AM, Daniel Bünzli wrote:
> Le mercredi, 27 avril 2016 à 11:22, William a écrit :
>> How are we supposed to handle this nicely with the standard library ? Is
>> there a better approach to catch those exceptions ?
> Capture them immediately around the function call that raises with a _ pattern.  
> For example:  
>
> match (try Some (List.hd lst) with Failure _ -> None) with
> | None -> Printf.printf "empty list\n"
> | Some hd -> …
>
> That's the way these functions should have been defined in the first place.
>
> Best,  
>
> Daniel
>
>
>
Or, with the new pattern matching with exception syntax :

match List.hd lst with
| exception Failure _ -> Printf.printf "empty list\n"
| hd -> ...


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27  9:43   ` Jacques-Henri Jourdan
@ 2016-04-27  9:51     ` Roberto Di Cosmo
  2016-04-27 10:00     ` Daniel Bünzli
  1 sibling, 0 replies; 11+ messages in thread
From: Roberto Di Cosmo @ 2016-04-27  9:51 UTC (permalink / raw)
  To: Jacques-Henri Jourdan; +Cc: caml-list

On Wed, Apr 27, 2016 at 11:43:20AM +0200, Jacques-Henri Jourdan wrote:
> Or, with the new pattern matching with exception syntax :
> 
> match List.hd lst with
> | exception Failure _ -> Printf.printf "empty list\n"
> | hd -> ...
> 

Pretty cool!

-- 
Roberto

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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27  9:43   ` Jacques-Henri Jourdan
  2016-04-27  9:51     ` Roberto Di Cosmo
@ 2016-04-27 10:00     ` Daniel Bünzli
  2016-04-27 10:04       ` Jacques-Henri Jourdan
  1 sibling, 1 reply; 11+ messages in thread
From: Daniel Bünzli @ 2016-04-27 10:00 UTC (permalink / raw)
  To: Jacques-Henri Jourdan; +Cc: caml-list

Le mercredi, 27 avril 2016 à 11:43, Jacques-Henri Jourdan a écrit :
> Or, with the new pattern matching with exception syntax :
>  
> match List.hd lst with
> | exception Failure _ -> Printf.printf "empty list\n"
> | hd -> ...

When was that introduced again ? I'm now starting to move to 4.01 language-wise.  

Daniel

   



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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27 10:00     ` Daniel Bünzli
@ 2016-04-27 10:04       ` Jacques-Henri Jourdan
  2016-04-28  7:25         ` Goswin von Brederlow
  0 siblings, 1 reply; 11+ messages in thread
From: Jacques-Henri Jourdan @ 2016-04-27 10:04 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 454 bytes --]

I think it has been introduced in 4.02.

On 04/27/2016 12:00 PM, Daniel Bünzli wrote:
> Le mercredi, 27 avril 2016 à 11:43, Jacques-Henri Jourdan a écrit :
>> Or, with the new pattern matching with exception syntax :
>>  
>> match List.hd lst with
>> | exception Failure _ -> Printf.printf "empty list\n"
>> | hd -> ...
> When was that introduced again ? I'm now starting to move to 4.01 language-wise.  
>
> Daniel
>
>    
>
>
>



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27  9:22 [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern William
  2016-04-27  9:28 ` David Allsopp
  2016-04-27  9:36 ` Daniel Bünzli
@ 2016-04-27 11:01 ` Gabriel Scherer
  2016-04-27 17:17   ` Adrien Nader
  2 siblings, 1 reply; 11+ messages in thread
From: Gabriel Scherer @ 2016-04-27 11:01 UTC (permalink / raw)
  To: William; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 1544 bytes --]

We now have a section of the reference manual (which has been updated to
4.03) on Warnings, and this particular warning is documented there:
  http://caml.inria.fr/pub/docs/manual-ocaml/comp.html#s:comp-warnings

Feedback (for example as Mantis tickets) on which warnings are confusing
and would deserve additional documentation -- or even patches to provide
this documentation -- are warmly welcome.

On Wed, Apr 27, 2016 at 5:22 AM, William <r.3@libertysurf.fr> wrote:

> Hello,
>
> with ocaml 4.03, I came with many warnings such as this one :
>
> let lst = [] in
> try
>    let fst = List.hd lst in
>    Printf.printf "fst:%s\n" fst
> with
> | Failure "hd" -> Printf.printf "empty list\n";;
>
> Characters 123-127:
> | Failure "hd" -> Printf.printf "empty list\n";;
>            ^^^^
> Warning 52: the argument of this constructor should not be matched against
> a
> constant pattern; the actual value of the argument could change
> in the future.
>
>
>
> It occurs typically for Failure "hd", Failure "tl", Failure
> "float_of_string", Failure "int_of_string", Failure "lexing: empty token",
> Invalid_argument "Filename.chop_extension", ...
>
> How are we supposed to handle this nicely with the standard library ? Is
> there a better approach to catch those exceptions ?
>
>
> Best regards,
>
> William
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 2502 bytes --]

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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27 11:01 ` Gabriel Scherer
@ 2016-04-27 17:17   ` Adrien Nader
  2016-04-27 18:20     ` Gabriel Scherer
  0 siblings, 1 reply; 11+ messages in thread
From: Adrien Nader @ 2016-04-27 17:17 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: William, caml users

On Wed, Apr 27, 2016, Gabriel Scherer wrote:
> We now have a section of the reference manual (which has been updated to
> 4.03) on Warnings, and this particular warning is documented there:
>   http://caml.inria.fr/pub/docs/manual-ocaml/comp.html#s:comp-warnings
> 
> Feedback (for example as Mantis tickets) on which warnings are confusing
> and would deserve additional documentation -- or even patches to provide
> this documentation -- are warmly welcome.

Thanks for the documentation update. I've found the warning fairly
confusing and this makes it clear.

As a small note, in a library I use (I won't name it because I haven't
checked if there were a version more recent than a couple years), C
bindings use the built-in Failure exception. I guess this is because it
is so much more convenient when it comes to making C bindings.
Am I right that such scenario will always trigger this warning because
the warning attribute will never be removed from the OCaml upstream
definition of Failure?

PS: my only comment about the warning is maybe to state that all(?)
exceptions from the stdlib have the corresponding attribute set.

-- 
Adrien Nader

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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27 17:17   ` Adrien Nader
@ 2016-04-27 18:20     ` Gabriel Scherer
  0 siblings, 0 replies; 11+ messages in thread
From: Gabriel Scherer @ 2016-04-27 18:20 UTC (permalink / raw)
  To: Adrien Nader; +Cc: William, caml users

[-- Attachment #1: Type: text/plain, Size: 2462 bytes --]

If a single call to the binding code can return several of Failure
payloads, then yes, I guess you cannot escape using specific text on the
warning text. You can either disable the warning locally using a @warning
attribute (if your backward-compatibility requirement do not go back before
ppx attributes), or move the string-testing logic to inside the
pattern-matching clause (a guard would also work). Note that the
string-testing solution *can* be made robust if the library/binding exports
identifiers containing the value of the error messages, and you test
against them.

Note that since Benoît Vaugon's optimization work in 4.02.0
pattern-matching on several literal strings is sensibly faster than a
series of string-equality test, so in performance-critical code it makes
sense to use pattern-matching on string -- but it does not need to be part
of the try-handler patterns directly, it can be only in the (Failure err ->
...) clause.

Thanks for your comments (and William's) on the documentation. Feel free to
add additional comments on the related ticket William created, PR#7245
  http://caml.inria.fr/mantis/view.php?id=7245

On Wed, Apr 27, 2016 at 1:17 PM, Adrien Nader <adrien@notk.org> wrote:

> On Wed, Apr 27, 2016, Gabriel Scherer wrote:
> > We now have a section of the reference manual (which has been updated to
> > 4.03) on Warnings, and this particular warning is documented there:
> >   http://caml.inria.fr/pub/docs/manual-ocaml/comp.html#s:comp-warnings
> >
> > Feedback (for example as Mantis tickets) on which warnings are confusing
> > and would deserve additional documentation -- or even patches to provide
> > this documentation -- are warmly welcome.
>
> Thanks for the documentation update. I've found the warning fairly
> confusing and this makes it clear.
>
> As a small note, in a library I use (I won't name it because I haven't
> checked if there were a version more recent than a couple years), C
> bindings use the built-in Failure exception. I guess this is because it
> is so much more convenient when it comes to making C bindings.
> Am I right that such scenario will always trigger this warning because
> the warning attribute will never be removed from the OCaml upstream
> definition of Failure?
>
> PS: my only comment about the warning is maybe to state that all(?)
> exceptions from the stdlib have the corresponding attribute set.
>
> --
> Adrien Nader
>

[-- Attachment #2: Type: text/html, Size: 3123 bytes --]

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

* Re: [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern
  2016-04-27 10:04       ` Jacques-Henri Jourdan
@ 2016-04-28  7:25         ` Goswin von Brederlow
  0 siblings, 0 replies; 11+ messages in thread
From: Goswin von Brederlow @ 2016-04-28  7:25 UTC (permalink / raw)
  To: Jacques-Henri Jourdan; +Cc: caml-list

On Wed, Apr 27, 2016 at 12:04:19PM +0200, Jacques-Henri Jourdan wrote:
> I think it has been introduced in 4.02.
> 
> On 04/27/2016 12:00 PM, Daniel Bünzli wrote:
> > Le mercredi, 27 avril 2016 à 11:43, Jacques-Henri Jourdan a écrit :
> >> Or, with the new pattern matching with exception syntax :
> >>  
> >> match List.hd lst with
> >> | exception Failure _ -> Printf.printf "empty list\n"
> >> | hd -> ...
> > When was that introduced again ? I'm now starting to move to 4.01 language-wise.  
> >
> > Daniel

That's rarther stupid in this case. Why create and catch an exception
when you can just match the list directly?

    match lst with
    | [] -> Printf.printf "empty list\n"
    | hd::_ -> ...

MfG
	Goswin

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

end of thread, other threads:[~2016-04-28  7:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-27  9:22 [Caml-list] ocaml 4.03 and warning 52 : argument of this constructor should not be matched against a constant pattern William
2016-04-27  9:28 ` David Allsopp
2016-04-27  9:36 ` Daniel Bünzli
2016-04-27  9:43   ` Jacques-Henri Jourdan
2016-04-27  9:51     ` Roberto Di Cosmo
2016-04-27 10:00     ` Daniel Bünzli
2016-04-27 10:04       ` Jacques-Henri Jourdan
2016-04-28  7:25         ` Goswin von Brederlow
2016-04-27 11:01 ` Gabriel Scherer
2016-04-27 17:17   ` Adrien Nader
2016-04-27 18:20     ` Gabriel Scherer

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).