If you don't mind the eager evaluation, you can even do it without macros. This way of doing things tends to compose much better: type BranchPair[T] = object then, otherwise: T # This cannot be a template yet, buggy compiler... proc `!`*[T](a, b: T): BranchPair[T] {.inline.} = BranchPair[T](then: a, otherwise: b) template `?`*[T](cond: bool; p: BranchPair[T]): T = (if cond: p.then else: p.otherwise) echo(true ? 3 ! 4)
- Ternary operator implementation example cdome
- Re: Ternary operator implementation example Udiknedormin
- Re: Ternary operator implementation example Araq