Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

reflection - Why doesn't Rebol 3 honor quoted function parameters that are parenthesized?

The DO dialect uses series of category PAREN! for precedence, and will usually boil away the underlying parentheses structure prior to invoking a function.

However, it used to be possible in Rebol 2 to specify in a function's definition that you wanted it to suppress evaluation of parentheses at the callsite. You did this by using a "literal word" apostrophe mark on a parameter:

evaluated: func [param] [probe param]

non-evaluated: func ['param] [probe param]

>> evaluated (1 + 2)
3

>> non-evaluated (1 + 2)
(1 + 2)

So you get passed a SERIES! category type, of class PAREN!...in this case with three symbolic elements inside: 1, +, 2. This does not work in Rebol 3:

>> non-evaluated (1 + 2)
3

Is this a bug or a purposeful design decision? Is there a workaround? Note that putting the quote operator at a callsite won't work, because then it's the symbolic word quote that gets quoted, and then the paren! gets evaluated on its own to become the final value of the expression :-/

>> non-evaluated quote (1 + 2)
quote
== 3
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The behaviour of this argument passing type has been changed on purpose. (Many users including myself requested the change). The advantage is that you can request evaluation for this argument type using parentheses (another way how to request evaluation is to use get-word). If you want truly unevaluated argument passing, see this:

quote: make function! [[
    "Returns the value passed to it without evaluation."
    :value [any-type!]
][
    :value
]]

That is again an improvement compared to R2, where such function does not really behave exactly the same way.

And in case you really want to pass a paren! to your function while not wanting to change its definition to use "truly unevaluated argument passing" you can try this:

non-evaluated (quote (1 + 2))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...