Re: Variable and function name space
Re: Variable and function name space
- Subject: Re: Variable and function name space
- From: Axel Luttgens <email@hidden>
- Date: Tue, 31 May 2011 14:02:33 +0200
Le 31 mai 2011 à 09:48, Adrian Nier a écrit :
> Is it normal for a programming language to have variables and functions in the same name space?
Well, behaviors may differ from one language to another, but there will often be some kind of clash.
For example, try to compile this C program:
int fun();
int fun()
{
return(1);
}
int fun;
int main()
{
fun();
}
> There were some hard to catch bugs for me over the years that involved a variable and a function having the same name. To never run into this problem again I decided to put underscores before variable names.
With AppleScript, a golden rule is perhaps to avoid to overcrowd the top-level name space (see hereafter).
> This also allows me to use names such as _class or _date which might otherwise be reserved keywords.
That's another point... ;-)
This may somehow be done with vertical bars as well, as with |class| or |date|.
> set doINeedToPutUnderscoresBeforeVariableNames to "Maybe"
>
> try
> return doINeedToPutUnderscoresBeforeVariableNames()
> on error
> return "You should start variable names with an underscore."
> end try
>
>
> on doINeedToPutUnderscoresBeforeVariableNames()
>
> return "You don’t need to put underscore before variable names."
>
> end doINeedToPutUnderscoresBeforeVariableNames
The question is to know whether doINeedToPutUnderscoresBeforeVariableNames needs to be a global variable...
In following example, variable "fun" and function "fun" are distinct:
on fun()
return "fun() called"
end fun
on main()
local fun
set fun to "fun"
fun()
end main
main()
That said, thanks to the "feature" you are complaining about, AppleScript allows, for the better or the worst, such tricks:
on fun()
return "fun() called"
end fun
set fun1 to fun
fun1()
Hmm... possibly a matter of bathwater and baby. ;-)
HTH,
Axel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden