2010-04-26 5 views
0

Dans Applescript, si vous déclarez un gestionnaire utilisant des paramètres étiquetés "with", les variables locales obtiennent les valeurs des arguments et les paramètres eux-mêmes ne sont pas définis. Par exemple:Dans Applescript, pourquoi les variables locales dans les gestionnaires capturent "avec" des paramètres étiquetés?

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 

résultats dans un message d'erreur « CSDN » ne soit pas défini dans la deuxième ligne de bam. thing et frst sont tous deux définis, obtenant les arguments transmis dans l'appel à bam. Pourquoi cela arrive-t-il? Pourquoi scnd est-il indéfini?

Remarque: Je sais que déclarer des variables comme "locales" dans un gestionnaire est inutile. C'est fait dans les exemples à des fins d'illustration.

Voici d'autres exemples qui ne génèrent pas d'erreurs, illustrant quelle variable obtient quelle valeur. Pour distinguer les premier et deuxième paramètres donnés, chaque gestionnaire est appelé with le premier paramètre donné et without le second paramètre donné. Notez que l'utilisation de la syntaxe given userLabel:userParamName n'a aucun problème avec la capture de valeur.

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)} 

Résultat:

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

Répondre

1

Après avoir joué avec elle un peu, la réponse semble être que l'aide des paramètres étiquetés with ne présente pas les variables. Au lieu de cela, les valeurs sont affectées aux variables locales dans l'ordre dans lequel elles sont rencontrées dans le corps du gestionnaire.

Des exemples:

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) } 

Résultat:

{ baa:{frst:true, scnd:false}, 
    bas:{frst:"set", scnd:false}, 
    qux:{frst:true, scnd:false}, 
    quux:{frst:true, scnd:false}} 
Questions connexes