Last active
October 11, 2017 14:57
-
-
Save nicolasboulay/a8ee4a65e8c2cc110c20e6d24e838e86 to your computer and use it in GitHub Desktop.
Sum type not always the best choice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When you came from the Ocaml world, tree and AST are easly done with a sum type. The sum type have the very nice feature of | |
not compiling if a case is missing, wich is super nice for code refactoring. | |
But a new kind of technique that only use module and function exist, it's called tagless final. Module are kind of golang package | |
with parameters. It's look like mother class but with types as parameters. | |
So you use simple function call to describe an ast, then you could interpret it the way you want. Typical examples are | |
type checking, pretty print and execution. | |
I don't think it's possible to do it in Golang with interface and struct because of the lack of type as parameter. | |
Maybe if a package could be created at compile time with some parameter, the technique became possible. | |
[ENG] http://okmij.org/ftp/tagless-final/index.html | |
[fr] https://linuxfr.org/users/kantien/journaux/tagless-final-ou-l-art-de-l-interpretation-modulaire |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment