3 ms·
>HN user jerf has a method of revealing intent through types in golang, which he calls something like microtyping. Basically, everything has its own type, based
by vram22 8y ago
>HN user jerf has a method of revealing intent through types in golang, which he calls something like microtyping. Basically, everything has its own type, based on its domain semantics. So if you're using a float64 to store time deltas, you don't just use the float64. Instead, you base a TimeDelta type on float64.
>type TimeDelta float64
>Then golang allows you to attach methods to that type, which can then provide more opportunities for intention revealing. Also, this prevents a programmer from mistakenly using a float64 representing width and shoving it into a time calculation.
IIRC, this technique, or something similar, is shown in The Go Programming Language book.
F# also allows you to do similar stuff, I think.
- nikofeyn 8y agof# definitely allows this with single case discriminated unions. and in f#, you can add members to any type, which includes discriminated unions, records, classes, etc. in fact, you can add member functions somewhere else after the definition of the type. so you can define the type, write functional helper functions against that type and then later add member functions to the type that are defined in terms of the helper functions.
- vram22 8y agoDidn't know that stuff, cool, thanks. (In early stage of learning F#). That feature you mention sounds a bit like Ruby's open classes, where, in file B (or even in file A), you can add a method to a class C (where C is defined in file A), outside of the definition of C. I was just reading somewhere else recently that some other language also has this feature, maybe it was about Lisp, in the recent thread about it.