bit of an odd question: Used to do lots of ```blah...
# cfml-general
m
bit of an odd question: Used to do lots of
Copy code
blah = structNew()
blah.a = 1
blah.b = 2
years ago. Now using more like
blah = { a = 1, b = 2}
. My question is which of these two this (newer way using inline struct literal) would translate to under the hood:
Copy code
// Door No. 1
blah = structNew()
blah.a = 1
blah.b = 2

// Door No. 2
_someSHAhashTemp = structNew() // just guessing here; the actual implementation isn't important, just the order of operations
_someSHAhashTemp.a = 1
_someSHAhashTemp.b = 2
blah = _someSHAhashTemp
That is, when the actual assignment operator is handled to set
blah
, is it given the fully-baked struct that already has keys
a
and
b
, or is it pointed at a new empty struct which is then populated?
b
Prolly depends on the engine
For BoxLang, we use a varargs constructor behind the scenes that struct literals compile to which creates a new struct with the given keys and values
Are you asking which key is added first?
In BoxLang, it would be in the declaration order, but wouldn't matter unless you had a linked struct literal like so
Copy code
foo = [ "brad" : "wood", "luis" : "majano" ];
m
I guess more asking if (in, say ACF) if struct literals are first class or if they're syntactic sugar over a Door No. 1, which would mean
blah
points, for a very brief time, to an empty struct.
b
I don't see how it matters
The reference isn't available until it's populated anyway
m
which shouldn't matter... unless you open pandora's box of shared mutable state bwahahaha
b
So no code could use the struct prior to its population
m
like the session scope
b
The struct literal declaration is an atomic operation so far as your code is concerned
the struct variable is not made available to you until it's populated
so there is no way to access it when it's still empty
m
ah, ok. my example was boiled down a bit too far. I'm looking at a case where it's actually more like
Copy code
blah = {c: 3, d: 4}
blah = {a: 1, b: 2}
and if blah can be seen by other than the local execution scope (like a concurrent request)
b
Well, I can't prove that with ACF. We could dig through Lucee's source to find out
m
it would be difficult to demonstrate, for sure. I'm trying to rule out a race condition where a struct that shouldn't be empty is ending up that way.
b
I know for certain Boxlang doesn't assign the struct into the scope until its creation is complete. I would be very surprised to learn Adobe or Lucee did anything different
It wouldn't be that hard to try and reproduce
m
it would seem like a problematic design decision if they went that way, for sure
thanks for letting me ask question and bounce ideas off you 🙂
b
@mithlond Here is a quick test that declares a struct literal with 5000 keys https://trycf.com/gist/acd9d8f3a516beb740daf5970ccfd7eb/acf2023?theme=monokai
👍 1
Another thread watches for the struct to appear in the request scope and immediately counts the keys
I get exactly 5000 keys every time (having run the test about 20 times now)
👍 1
The same code works on Lucee, but you have to lower the number of keys or you get a "method too large" error from the bytecode generation