elvish icon indicating copy to clipboard operation
elvish copied to clipboard

Extend assoc and dissoc to work on namespaces

Open hanche opened this issue 4 years ago • 2 comments

Since namespaces are so very similar to maps, it seems useful to have assoc and dissoc work on namespaces as well, with the obvious semantics.

Incidentally, assoc works on strings, although that is not documented:

⬥ assoc abc 1 x
⮕ axc

hanche avatar Jan 15 '22 18:01 hanche

@hanche: Do you have a real world use case for treating namespaces in the manner you propose? It's probably justified if only for orthogonality with other objects handled by those two commands. But it's easier to justify such a change if there is an actual use case. Especially since there are likely to be unexpected side-effects of such a change.

krader1961 avatar Feb 18 '22 02:02 krader1961

My primary use case, for now, is to help build a namespace to feed to eval. I'd like to write this (simplified – code for &on-end condition omitted):

fn . {|&ns=(ns [&]) file @args|
  eval (slurp <$file) &ns=(assoc $ns args $args)
}

My current code converts the given namespace to a map, adds the args item to it, and converts the result back to a namespace once more. I would have liked to avoid that.

hanche avatar Feb 18 '22 10:02 hanche