[BLOG] Effective ML Through Merlin's Destruct Command

I’m very pleased to present you an article with a collection of small illustrations and examples of how to use the destruct command to generate patterns in the presence of pattern matches.

The command has been present in Merlin for several years (and accessible via OCaml-LSP) but, as the various changelogs relating to Merlin mention, we have spent some time polishing it and adapting it to the evolutions of OCaml, making it more stable (essentially in the presence of punning) and taking into account the changes made to the representation of functions (and their parameters).

The aim of the article is to show how the destruct command works in a number of very concrete cases, and ends with an example (a little artificial for the purposes of the article and for teaching purposes) which shows how to use destruct interactively.

Can’t wait to hear your feedback! Happy reading!


Thanks for this great article, and destruct command of course. One thing that would make the command even better in my use cases would be the ability to configure the right-hand side of the match, because the current joker term (underscore character) prevents compilation (for instance, I’d rather add a failwith "not yet implemented"). Is this the kind of thing you could consider implementing?

EDIT: should you implement such an option, you might even allow the RHS to contain a special lexical token (e.g. __PATTERN__ or whatever) standing for the case, so that we could use it in the set option (e.g. failwith "__PATTERN__ not implemented yet"), then and it would be substituted by the said case by destruct.


It would be cool if “destruct” on an expression inside match ... with would try to fix the existing pattern match, rather than introducing a nested match match ... with ... with.

That’s great read, thanks for posting.

A similar feature I find useful is autocompleting module based on module types. In VS Codium, if you have a module type A, you can type module F : A = _ to get an autocompleted struct ... end with all types/val that appear in the signature. This is especially useful given that the LSP likes to report errors on the whole module when some types/val are missing.

Unfortunately that doesn’t seem to work with functor arguments though, it just autocomplete with struct end.


It is a very nice idea that probably need some design in order to be efficient (and non-intrusive). Thanks for the reading (and the suggestion)!

Oh yes, it seems possible to find if the parent expression is a pattern enclosed into a match/function expr and try to reduce noisy generation! Thanks for the feedback (and the reading)!

Yes, it could be useful, even it is not “totally related to destruct” because destruct is designed to deal with patterns. But yes, it is obviously a nice idea for merlin, in order to improve te way that we write code. Thanks a lot!


This is also a merlin feature, called “construct”! It creates a value for a hole based on the inferred type of the hole (and it should work in other contexts as well). I think it’s not as well known as destruct, and could make for a nice blog post.


Yes, it’s an obvious improvement, but what about opening an issue on the repo so that it can be tracked (and credit for the request can be given to you)?