Closed philrz closed 3 months ago
Verified in Zed commit 18b3c7f.
Our Zed program with the user-defined function defined within the scope of the user-defined operator now parses successfully and produces the expected result.
$ zq -version
Version: v1.14.0-19-g18b3c7f8
$ cat attempt.zed
op square(val): (
func compute(num): (
num * num
)
yield compute(val)
)
$ echo '2' | zq -I attempt.zed 'square(this)' -
4
Thanks @mattnibs!
Amazing! Thank you for implementing this!
tl;dr
In the Zed docs about statements several times we explain scopes as:
However, there are valid reasons why a user might want to treat other
( )
-enclosed code blocks (e.g, the body of a user-defined operator) like scopes.Details
Repro is with Zed commit f1be6a4.
This topic was kicked off by a community user in a Slack thread. In their own words:
There was some back & forth about what was implied by the word "inline" here (e.g., in language contexts it often refers to this kind of concept from C++, which indeed did not seem to be what this user had in mind). I ultimately read back this simplified example that the user confirmed captured the essence of what they had in mind.
This works:
However, this currently does not, as it produces a syntax error.
In terms of why the user might find the second approach desirable, they confirmed:
Indeed, other languages have this concept of being able to define functions, constants, etc., that are local to the innermost scope, so it's understandable why users might hope to see it in Zed.
That covers a specific case of a
func
inside of anop
, but if we want to approach this in a general way, it seems there's other variations we might want to cover, e.g., #5075 tracks the equivalent for user-defined functions.