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 

の2行目に「scnd」が定義されていないというエラーメッセージが表示されbamます。thingfrstは両方とも定義されており、への呼び出しで渡された引数を取得しますbam。なぜこうなった?なぜscnd未定義なのですか?

注:ハンドラー内で変数を「ローカル」として宣言する必要がないことは知っています。これは、説明のために例で行われています。

エラーをスローしないその他の例をいくつか示し、どの変数がどの値を取得するかを示します。最初に指定されたパラメーターと2番目に指定されたパラメーターを区別するために、各ハンドラーはwith最初に指定されたパラメーターとwithout2番目に指定されたパラメーターを呼び出されます。構文を使用しても、値の取得に問題はないことに注意してください。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 に答える