Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pattern-matching of arrays in lets should not print warnings #6034

Closed
vicuna opened this issue Jun 7, 2013 · 6 comments
Closed

pattern-matching of arrays in lets should not print warnings #6034

vicuna opened this issue Jun 7, 2013 · 6 comments

Comments

@vicuna
Copy link

vicuna commented Jun 7, 2013

Original bug ID: 6034
Reporter: @lefessan
Status: resolved (set by @xavierleroy on 2013-06-07T17:01:19Z)
Resolution: suspended
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)

Bug description

Pattern-matching an array in a let always causes a Warning 8:

let [| a;b;c |] = Array.init 3 (fun i -> i);;

Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[| |]
val a : int = 0
val b : int = 1
val c : int = 2

and a bad array length triggers a Match_failure:

let [| a;b;c;d |] = Array.init 3 (fun i -> i);;

Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[| |]
Exception: Match_failure ("//toplevel//", 1, 4).

I would prefer to (1) have a different warning number that is disabled by default and (2) raise Exception: Invalid_argument "index out of bounds"
instead of Match_failure, for the specific case (let [| ... |] = ... ).

@vicuna
Copy link
Author

vicuna commented Jun 7, 2013

Comment author: @gasche

How would having a different behavior just for arrays make sense?

@vicuna
Copy link
Author

vicuna commented Jun 7, 2013

Comment author: @lefessan

Ok, let's come back to the origin: I want to generate a huge set of symbolic names for values coming from an array. Of course, I can do:

let foo = t.(0)
let bar = t.(1)
etc.

but I think it would be much nicer to have:

let [| foo; bar; ... |] = generate_values ...

especially as the users will probably use this idiom a lot in their programs.

At the same time, I don't want to disable warnings for standard pattern-matching, so I am proposing this solution, in case somebody else would find that they have the same problem and would be interested with the same solution.

Of course, I am interested in any other solution, or to generalize to some other types than arrays.

By the way, I am not fond of the solution that I am currently using:
let (foo, bar, ...) = Obj.magic (generate_values ...)

@vicuna
Copy link
Author

vicuna commented Jun 7, 2013

Comment author: @gasche

At the same time, I don't want to disable warnings for standard pattern-matching, so I am proposing this solution, in case somebody else would find that they have the same problem and would be interested with the same solution.

I once had the same issue for lists:

let [foo;bar;baz] = List.map wrap [foo'; bar'; baz']

my solution was to develop a syntax extension (uploaded here, courtesy of 2007)
http://gallium.inria.fr/~scherer/tmp/refutable/

I think a pragma to locally-disable refutability warning would be much, much nicer than a type-directed approach. That's at arm's length in Alain's extension_points branch.

By the way, I am not fond of the solution that I am currently using:
let (foo, bar, ...) = Obj.magic (generate_values ...)

Indeed, it's the ugliest code I've seen today yet :-)

@vicuna
Copy link
Author

vicuna commented Jun 7, 2013

Comment author: @xavierleroy

(2) raise Exception: Invalid_argument "index out of bounds"
instead of Match_failure, for the specific case (let [| ... |] = ... ).

This would be backward-incompatible. All this breakage and effort for a minor stylistic issue in generated code. I move to close/no-change-required.

@vicuna
Copy link
Author

vicuna commented Jun 7, 2013

Comment author: @lefessan

This would be backward-incompatible.

For me, anybody who would put a "try ... with Assert_failure ->" around a non-exhaustive "let" instead of using "match with" in the first place would deserve to see its code broken...

All this breakage and effort for a minor stylistic issue in generated code.

It is not generated code, but an idiom when creating WxWidgets applications in OCaml, users have to define lots of symbolic variables for widgets identifiers, and writting list of "let toto_id = t.(..)" is both tiring, unreadable and error-prone. I was just looking for a way to make it nicer to read.

I think a pragma to locally-disable refutability warning would be much, much nicer than a type-directed approach. That's at arm's length in Alain's extension_points branch.

Sounds like a good idea !

@vicuna
Copy link
Author

vicuna commented Jun 7, 2013

Comment author: @xavierleroy

For me, anybody who would put a "try ... with Assert_failure ->" around a non-exhaustive "let" instead of using "match with" in the first place would deserve to see its code broken...

Agreed, but it's an incompatible change nonetheless, and as a matter of policy we don't do this unless there are really strong reasons.

(And no open bugs for a forthcoming release, which is definitely not the case right now. Go back triaging!)

I think a pragma to locally-disable refutability warning would be much, much nicer than a type-directed approach. That's at arm's length in Alain's extension_points branch.

Indeed. So, it's "resolved/suspended" until we make progress on the "extension points" approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant