A simple approach to locally scoped facts; asking for feedback

vars are visible to included modules

vars: take you a long way, but given they are visible to all included roles, they are not local in the sense that variables defined in a function in typical programming languages are. This is a hindrance to naming things generically, as there seems to be no way to distinguish between vars passed directly to your role, and vars by the same name that happened to be set higher up the inclusion chain, and the more generic your name, the likelier someone used it in a conflicting context.

undef() can help

I did just find out there is a cumbersome and error-prone way to work around this particular issue - the undef() function - that the caller can use to unset any settable vars if he wishes to not pass them. I actually found a use for it improve the api of my collection.

other vars limitations

I think I found a somewhat clean api to accomplish setting local facts that behave as I expect, but I don’t see a way to do this for vars:

  • I see no way to automatically undef() all vars; I can’t iterate over them.
  • Vars are evaluated lazily, so by the time they are evaluated, vars defined in terms of other vars may have values assigned to them in a conflicting context.

Only facts can be made local at the moment

I think the proposal you linked covers these issues, as you mention locally scoped vars and eager evaluation. I hope at least those parts can be integrated!

Until then, I see no other option for local definitions besides setting facts.