Thanks @Vindaar and @jyapayne... I can't keep up! 🙂
I was still mapping @Vindaars suggestion into a more general **Lazy** template
and @jyapayne anticipated my desire to reduce the redundant boilerplate even
further!
I will try to add his latest refinements to what I currently have.
# Lazy object member accessor support
template lazy*(
obj, ObjectType,
accessorName,
m_name, m_nameType,
body: untyped): untyped =
proc accessorName*(obj {.inject.}: var ObjectType): m_nameType =
if isNone obj.m_name:
result = body
obj.m_name = some result
else:
result = obj.m_name.get
# Sample usage of lazy access to user defined enviroment
import options, std/envvars
type
Env = object
m_user: Option[string]
m_box_sync_dir: Option[string]
m_downloads_dir: Option[string]
var env*: Env
lazy env, Env, user, m_user, string,
"USER".getEnv
lazy env, Env, box_sync_dir, m_box_sync_dir, string,
"/Users/" & env.user & "/Library/CloudStorage/Box-Box"
lazy env, Env, downloads_dir, m_downloads_dir, string,
"/Users/" & env.user & "/Downloads"
# Show that it actually works
when isMainModule:
echo "user : ", env.user
echo "box_sync_dir : ", env.box_sync_dir
echo "downloads_dir : ", env.downloads_dir
Run
I will post an updated version for your comments.
**Thanks again for your valued assistance.**