Guarded methods in OCaml
16 points by xvw
16 points by xvw
I don’t know of any mainstream languages that allow their definition.
Python’s type system does, actually! https://mypy.readthedocs.io/en/stable/more_types.html#advanced-uses-of-self-types
These are especially useful for mixins, IME.
Nicely written @xvw.
How well does OCaml present the error messages when you're missing the right type equality witness? You'd probably want the type error to also include the bindings in scope, like merlin has.
Thinking about the other pieces OCaml has lying about, combining this with modular implicits might get you closer to Scala experience. The fun thing with Scala is the complex implicit discovery that usually trips me up when the types get more complex.
Thanks.
To be fair, the goal of the article was mostly to give a concrete example about Type Equality Witness using GADTs (since objects are not really used in OCaml), different than the usual one (Typed AST). But yes, having Refl + Implicits is a must have :)
Yes, Scala supports this as well and even more elegant I would argue. A language that goes even one step beyond and makes it a first-class concept is then Idris (and similar dependently-typed languages).
Yes but it is ... recent, since until Scala 3, the support of GADTs, in Scala, was a little bit messy. I gave a presentation (in French, sorry) about that, but the support is in English: https://docs.google.com/presentation/d/e/2PACX-1vRElFi7ljZaHskI860HZGO9a6fD0T5B4a-QSG5fKNk76DxEVskzPbq3CFPHnRGFC6xRftOA7FKs1l9R/pub?start=false&loop=false&delayms=60000