I love how nim lets me get away with so much at compile time. Today, I made a distinct datatype with a custom numeric literal.
type Semicircle16* = distinct int16
proc `'sc16` *(s: string): Semicircle16 = Semicircle16(parseInt(s))
I wanted to make sure the literal was bounds checked to within the range of an int16. I realized that since the literal is coming from source code (constant at compile time), I could make the bounds check happen at compile time, too.
proc `'sc16` *(s: static string): Semicircle16 =
const n = parseInt(s)
Semicircle16(n)