- Author
- Not stated (site: xvw.lol)
- Published
- 2025-06-29
- Kind
- Personal technical note, translated from French, with working code
- Original
- xvw.lol/en/articles/oop-refl.html
- Subject
- Programming languages and types
What it is
The page names no author and is a translation of a note in French. It starts from slides by Gabriel Scherer on object-oriented and functional symmetry.
What it says
- Guarded methods put constraints on the receiver for single methods, callable only when they hold.
- OCaml has no syntax for guarded methods, but a type equality witness can encode them.
- The witness is a type eq with one constructor, Refl, buildable only for equal types.
- A flatten method takes a witness that the element type is a list.
- A sum method takes a witness that the element type is int.
- Calling sum on a list of lists fails to compile with a type error.
- Static methods and Kotlin-style extension methods are alternatives, but both are defined outside the class.
What evidence it gives
The note gives OCaml code for an object list class with flatten and sum methods. It shows assertions and the compiler error for a wrong call. It reports no wider tests.
Interests and limits
This is a personal note on one example. The author says OOP is rarely encouraged in OCaml, so this is probably not useful. The code was not run for this page. The author is not named.