wryun / es-shell

es: a shell with higher-order functions
http://wryun.github.io/es-shell/
Other
313 stars 26 forks source link

How to expand quoted patterns? #32

Closed hyphenrf closed 2 years ago

hyphenrf commented 3 years ago

My specific encounter was implementing a ghetto switch-case #31 like so:

let( match = something
     cases = some* thing onething $match #last one is 'default'
     bodies = ( {echo this is something}
                {echo this is thing}
                {echo this is onething}
                {echo reached default} ))
for (case = $cases; body = $bodies) if {~ $match $case} $body

but this will echo reached default because 'some*' won't be expanded inside the tilde-match statement.
this is good behaviour of course, but my question is do we have an idiomatic way to override it?

AFAIK there's no expand primitive, and eval would try to run the pattern as a command.
Perhaps the only answer is eval '{~ $match '$case'}', but I wonder if there's something more general to expand quoted patterns without necessarily running a command on them under eval..