En Applescript, ¿por qué variables locales en los manipuladores de captura “con” parámetros marcados?
-
01-10-2019 - |
Pregunta
En Applescript, si se declara un controlador utilizando "con" parámetros marcados, las variables locales obtienen los valores de los argumentos y las mismas parámetros no están definidos. Por ejemplo:
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
resultado en un mensaje de error "scnd" no está definida en la segunda línea de bam
. thing
y frst
se definen tanto, conseguir los argumentos pasados ??en la llamada a bam
. ¿Por qué está pasando esto? ¿Por qué no está definido scnd
?
Nota: Sé que la declaración de variables como "local" dentro de un manejador es innecesaria. Está hecho en los ejemplos para propósitos ilustrativos.
Aquí hay algunos ejemplos más que no generan errores, lo que ilustra lo variable obtiene qué valor. Para distinguir entre la primera y la segunda dadas parámetros, cada controlador se invoca with
el parámetro primero dado y without
el parámetro segundo dado. Tenga en cuenta que el uso de la sintaxis given userLabel:userParamName
no tiene problemas con el valor de captura.
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)}
Resultados:
{ foo:{frst:true, scnd:false}, bar:{frst:true, scnd:false}, baz:{frst:true, scnd:false}}
Solución
Después de jugar con él un poco, la respuesta parece ser que el uso de parámetros with
etiqueta no introduce variables. En cambio, los valores se asignan a las variables locales en el orden en que se encuentran en el cuerpo manejador.
Ejemplos ilustrativos:
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) }
Resultados:
{ baa:{frst:true, scnd:false}, bas:{frst:"set", scnd:false}, qux:{frst:true, scnd:false}, quux:{frst:true, scnd:false}}