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
Comments
Comment author: @gasche How would having a different behavior just for arrays make sense? |
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) 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: |
Comment author: @gasche
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) 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, it's the ugliest code I've seen today yet :-) |
Comment author: @xavierleroy
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. |
Comment author: @lefessan
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...
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.
Sounds like a good idea ! |
Comment author: @xavierleroy
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!)
Indeed. So, it's "resolved/suspended" until we make progress on the "extension points" approach. |
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 [| ... |] = ... ).
The text was updated successfully, but these errors were encountered: