Sean Corfield wrote:

> isDefined() uses the standard CF scope lookup rules that have been
> around for a long, long time.

Yes, but the one of the reasons used to in persuading people to scope
their variables properly other than good practice was so that
isDefined() only checked a specific variable in a specific scope, not
every possible conotation of that variable and scope.  This is something
that was touted a long long time ago, before I remember your good self
being around.

Knowing that as CTO you will know better than me the inner workings of
CF, are you saying that CFMX does not do this?

> and in general when you scope a variable, you know
> the variable exists in that scope.

Thats just not true.  There will always be situation where variables do
not exist.  Its simply not practicable or sensible to define every
single variable that an application will ever use at any point in its
entire lifetime, plus there are 3 scopes that you cannot and must not
assume existance of your application's required variables.  The easiest
way to tell if  the user has "been this way before" is to check to see
if required, persistent variables are already in existance.

In fact, come to think of it, there are only 2 scopes that you can
safely assume exist in the first line of your code. These are SERVER and
CGI.  All other scopes and, therefore, those structures may not exist.

> > This would means that scoping is not really taken into account by
> > coldfusion, other by pure luck that functions like isDefined() look in
> > the null scope first.
>
> Sure, scoping is taken into account - and there is no "null scope".
>
I think you misunderstand what I mean by "null" scope.  The lookup rules
must be looking at a variable _without a scope_ before it starts looking
through the other scopes. This is what I mean by a null scope.

> Everything lives in a scope.

Total aside - I did find in CF5 that a number of the scopes were
actually available via the variables scope as well directly through
their own scope. I did find this little fact useful on a couple of
occasions, though it now escapes me why.

> The lookup rules are a convenience so
> that you (mostly) don't have to specify "variables." everywhere.

Grrr, but that is why I do specify scopes everywhere, unless I slip and
get lazy (variables scope only), so that CF does what its told and
doesn't decide to trundle off through all the scopes.  Maybe I'm still
stuck in CF3/4/5 land too much.

> > If isDefined() is not taking scopes into account, does it keep going
> > until its checked all scopes or does it stop searching when it hits the
> > first occurance?
>
> First off, it *is* taking scopes into account - according to the
> variable lookup rules -

See comment about "null scopes" to realise that, no, it is not taking
scopes into account, only *appearing* to take scopes into account. If it
was taking scopes into account, then it would check variables.myvariable
and stop, not trundle through all the scopes looking for
variables.variables.myvariable, form.variables.myvariable, etc....

> and secondly, yes, it stops as soon as it
> finds a defined path to a variable. That's why I said the difference
> is only visible for *un*defined variables (or where a variable is
> defined in a scope other than the 'closest' scope).

Thought it was - I was just being inflammatory... (I think thats the
right word)
Sorry... one of those days...

> > <cfif IsDefined("variables") AND
> > StructKeyExists(variables.variables,"test")>
>
> That's wrong on two levels:
> 1. variables is *always* defined because it is a scope
> 2. if variables.variables doesn't exist you'll get an exception!

ok - the whole using a variable called "variables" was just way too
confusing, but I was trying to follow on with the examples that had
previously been given and it all got a bit out of hand.

> No, that's the whole point - you *don't* need to use isDefined()
> because you *know* the built-in scopes exist.

So are you saying that every scope all ready exists in CFMX as a
structure, even if that scope is empty?

Does this hold true for previous versions of CF?  I'm still mostly on
CF5. Don't fix what ain't broke, particularly if the client is happy.

> > I understand what is happening here, but it means that
> variables.foo.bar
> > doesn't exist either, that "foo.bar" can only be referenced by
> > variables['foo.bar'] and CF is letting people create variables with
> > invalid characters in their names.
>
> CF has always allowed variables with . in them. In fact that was one
> of the specific differences between CF5 and CFMX:
>
> CF5:
> <cfset foo.bar = 42>
> Creates a variable called foo.bar
>
> CFMX:
> <cfset foo.bar = 42>
> Creates a variable called foo as a struct with a key called bar

Gah! I hate being on the fence between CF5 and CFMX.  My brain does both
versions at the same time.  I write for CF5, but take into CFMX and vice
versa. I can't remember what I can and can't do going back to CF5 sometime!

In this case I was thinking specifically of CFMX for exactly the reason
you state as being the difference between CFMX and CF5. Dot notation
creates structures in CFMX.
So :

> > Do <cfset variables['foo.bar'] = 42 /> and then try
> > isDefined('foo.bar') vs structKeyExists(foo,'bar') (the latter will
> > fail *before* calling structKeyExists() because foo is not defined as
> > a variable).

What you're saying here is that not only does isDefined() not take into
account scopes (as argued above), it also has an added layer where
having checked for the structure foo and the key bar in all the scopes
and not found it, it now looks for the variable  "foo.bar" in all the
scopes too?

Sorry to bang on about this, but isDefined() appears not to be working
the way I have, perhaps mistakenly, understood in the past from the
various book writing luminaries of the mailing lists. Also as a mainly
CF5 developer I have to be sure that isDefined() is doing the right
thing and that structKeyExists is _only_ a CFMX short cut.  Perhaps I'll
go back to ParameterExists() ;oD

Regards

Stephen
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to