0

在 Applescript 中,如果您使用“with”标记的参数声明处理程序,则局部变量会获取参数的值,而参数本身是未定义的。例如:

on bam of thing with frst and scnd
    local eat_frst
    return {thing: thing, frst:frst, scnd:scnd} -- this line throws an error
end bam
bam of "bug-AWWK!" with frst without scnd 

导致错误消息“scnd”未在bam. thing并且frst都被定义,获取在调用中传递的参数bam。为什么会这样?为什么是scnd未定义的?

注意:我知道在处理程序中将变量声明为“本地”是不必要的。出于说明目的,它在示例中完成。

这里还有一些不会引发错误的示例,说明什么变量得到什么值。为了区分第一个和第二个给定参数,每个处理程序都被调用with第一个给定参数和without第二个给定参数。请注意,使用该语法在值捕获方面没有问题。given userLabel:userParamName

on foo of thing given frst:frst_with, scnd:scnd_with
    local eat_nothing
    return {frst:frst_with, scnd:scnd_with}
end foo

on bar of thing with frst and scnd
    local eat_frst
    return {frst:eat_frst, scnd:scnd}
end bar

on baz of thing with frst and scnd
    eat_frst
    local eat_scnd, eat_others
    return {frst:eat_frst, scnd:eat_scnd}
end baz

{foo:(foo of "foo" with frst without scnd), ¬
 bar:(bar of "bar" with frst without scnd), ¬
 baz:(baz of "baz" with frst without scnd)}

结果:

{ foo:{frst:true, scnd:false},
  bar:{frst:true, scnd:false},
  baz:{frst:true, scnd:false}}
4

1 回答 1

1

在玩了一会儿之后,答案似乎是使用with标记参数不会引入变量。相反,这些值按照它们在处理程序主体中遇到的顺序分配给局部变量。

说明性示例:

on baa of thing with frst and scnd
    scnd
    frst
    return {frst:scnd, scnd:frst}
end baa

on bas of thing with frst and scnd
    -- note that eat_frst gets the value of the frst parameter,
    -- then gets set to "set"
    set eat_frst to "set"
    eat_scnd
    return {frst:eat_frst, scnd:eat_scnd}
end bas

on qux of thing with frst and scnd
    if scnd then
    end if
    local eat_scnd, eat_others
    return {frst:scnd, scnd:eat_scnd}
end qux

on quux of thing with frst and scnd
    if frst then
    end if
    if eat_scnd then
    end if
    return {frst:frst, scnd:eat_scnd}
end quux

{  baa: (baa of "baa" with frst without scnd), ¬
   bas: (bas of "bas" with frst without scnd), ¬
   qux: (qux of "qux" with frst without scnd), ¬
  quux: (qux of "qux" with frst without scnd) }

结果:

{ baa:{frst:true, scnd:false},
   bas:{frst:"set", scnd:false},
   qux:{frst:true, scnd:false},
  quux:{frst:true, scnd:false}}
于 2010-04-26T01:49:15.127 回答