Skip to content

Instantly share code, notes, and snippets.

@mratsim
Created March 20, 2018 12:56
Show Gist options
  • Save mratsim/1162668aee9efb0026c135b4ee974842 to your computer and use it in GitHub Desktop.
Save mratsim/1162668aee9efb0026c135b4ee974842 to your computer and use it in GitHub Desktop.
static + macro workarounds
import typetraits
type
BitsHolder[bits: static[int]] = object
template `div`[bits: static[int]](bh: typedesc[BitsHolder[bits]], n: static[int]): typedesc =
BitsHolder[bits div n]
type
MpUintImpl*[bh] = object
when bh is BitsHolder[128]: hi*, lo*: uint64
elif bh is BitsHolder[64]: hi*, lo*: uint32
elif bh is BitsHolder[32]: hi*, lo*: uint16
elif bh is BitsHolder[16]: hi*, lo*: uint8
# else:
# hi*, lo*: MpUintImpl[bh div 2]
MpUint*[bits: static[int]] = MpUintImpl[BitsHolder[bits]]
var a: BitsHolder[16] div 2
echo $a.type.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment