Would it be possible to write list.findBy(key, value)
macro, so that:
let people = @[(name: "John"), (name: "Sarah")]
echo people.findBy("name", "John")
Ideally it should validate "name" at compile time.
I tried some code, but it doesn't work, play:
import macros, options
macro findBy*[T](list: seq[T], field: string, value: untyped): Option[T] =
quote do:
for v in list:
if v.`field` == value: return
let people = @[(name: "John"), (name: "Sarah")]
echo people.findBy("name", "John")