brimdata / zed

A novel data lake based on super-structured data
https://zed.brimdata.io/
BSD 3-Clause "New" or "Revised" License
1.34k stars 67 forks source link

Make the body of a user-defined function a fully-fledged scope #5075

Open philrz opened 3 months ago

philrz commented 3 months ago

tl;dr

In the Zed docs about statements several times we explain scopes as:

...the main scope at the start of a Zed program or a lateral scope defined by an over operator

However, there are valid reasons why a user might want to treat other ( )-enclosed code blocks (e.g, the body of a user-defined function) like scopes.

Details

Repro is with Zed commit f1be6a4.

5073 has the background for the equivalent concept as relates to user-defined operators. It seems a similar enhancement might benefit user-defined functions as well. For example, this currently works:

$ cat circ.zed 
const pi = 3.14
func circ(r): (
  2 * pi * r
)

$ zq -version
Version: v1.14.0-17-gf1be6a4a

$ echo '10' | zq -I circ.zed 'circ(this)' -
62.800000000000004

But this does not:

$ cat circ-inner.zed 
func circ(r): (
  const pi = 3.14
  2 * pi * r
)

$ echo '10' | zq -I circ-inner.zed 'circ(this)' -
zq: error parsing Zed in circ-inner.zed at line 2, column 9:
  const pi = 3.14
    === ^ ===