Hacker News new | past | comments | ask | show | jobs | submit login
Alternatives to sum types in Go (pusher.com)
5 points by willsewell on Jan 31, 2018 | hide | past | favorite | 2 comments



Good post! Crossposting my comment from that other crusty news aggregator:

Alternative 1a is to use Alternative 1 with https://github.com/BurntSushi/go-sumtype /plug

go-sumtype requires the interface to be sealed (which you're already doing) and one small annotation:

    //go-sumtype:decl TheInterfaceName
Then you just run `go-sumtype`

    $ go-sumtype $(go list ./... | grep -v vendor)
and it will do exhaustiveness checks in any type switch in which `TheInterfaceName` participates. This will prevent the "For example, during a refactor a handler might be removed but a type that implements the interface is not." failure mode mentioned in the article.


Somewhat off topic, but the idea to show the examples as diffs against the prior one was based on this block post: https://jameshfisher.com/2017/11/23/diff-views.

I would be interested to hear people's thoughts on the effectiveness. I like how it is clear what is important in each example, but there is perhaps too much noise and it's a shame the regular syntax highlighting does not work.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: