I have just started playing with nim and like it enormously so far.
I have written a nimble module 'elvis' to expose some syntactic sugar for working with truthy, ternary and conditional assignment operators which is avialable https://github.com/mattaylor/elvis . All is looking good and working well but any advice from more experienced nim folks (nims?, nimers?, nimblers?) would be much appreciated.
I would also like to add a conditional access operator 'a .? b' which only invokes the right operand if the left operand is 'truthy' and otherwise returns am uninitiated instance of whatever a.b would have returned.
I have trying with templates like..
template `?.`*[T,U,V](left: T, right: proc (x: T, y: U): V): V =
if ?left: right(left) else: (var r:V)
template `?.`*[T,U](left: T, right: U): U =
if ?left: left.right else: (var r:U)
But the compiles seems to be getting confused when I call it eg..
nim> import elvis
nim> assert ?""
false` [AssertionError]
nim> ""?.len
template/generic instantiation from here
../../Volumes/Mat/work/nimby/elvis/elvis.nim(50, 35) Error: invalid type: 'HSlice[len.U, len.V]' in this context: 'proc (x: HSlice[len.U, len.V]): int{.inline, noSideEffect.}' for var
Not sure what I am doing wrong here.
Quick updated - I now have this sorta working now by removing the second template and rewriting the first as
template `?.`*[T,U](left: T, right: proc (x: T): U):U =
if ?left:
right(left)
else:
var res: U
res
let s = @["one"]
assert((s[0]?.len) == 3)
assert((s[1]?.len) == 0)
Still needs some extra love to support more use cases (varargs etc..)