This is a fairly common request. But J Andrew Lipscomb’s example is the best way to do the same thing in Xojo. While the With - End With seems convenient, I find the the Xojo way to be a bit more explicit especially if you happen to have a long method that requires scrolling (in which case it seems to be a bit easier to read). Opinions vary on this, naturally, but I don’t miss With - End With.
Instead of “With”, I would love a new keyword for Xojo for declaring smart macro substitutions like “alias”:
[Local/Global] Alias (as | = )
The default scope is local.
Use:
Alias st as Window1.TextArea1.StyledText // Just a macro substitution instruction to the compiler
st.Bold = True
st.Italic = True
st.Underline = True
//
Alias st as Window1.TextArea2.StyledText // reusable, from now on st gets another value
st.Bold = True
st.Italic = True
st.Underline = True
//
Alias x§ as 3.1415926 * p / 2
Dim n1 As Single = x(98.7) // Same as writing 3.1415926 * 98.7 / 2
Dim n2 As Single = x(32.1)
[quote=40846:@Rick Araujo]It’s easy to understand:
Dim num as Long
You must agree that we allocated space for num, fill it with zeros and when out of scope it will need to be deallocated.
Allocation consumes memory space, initialization and deallocation consumes CPU.
Dim st as Object // allocates a reference . ;)[/quote]
If it were strings, I would agree with you but here you’re talking about 8 or 4 bytes. I don’t remember the size of the stack but I’m sure that even a few dozens of references or Integer, Double or Int64 doesn’t make [edited] a big difference.
I always cache references to object in my code as much as I can, because I find it clearer, and I’m almost certain that accessing a local variable is much more CPU efficient than trying to retrieve again and again the same object in a namespace or in a Window/ContainerControl, etc…
But, I’m no expert in compiler stuff. The last assembler code I wrote was for Z80, just a few ( Coughing ) years ago.
On OS X, the main thread’s stack is 8MB and Xojo stacks default to 512KB.
It certainly can be more efficient, but my recommendation has always been to focus on code clarity and correctness. Then if things show up on performance traces, optimize.
[quote=40747:@Michael Diehr]The problem with JAL’s version :
is that you now have an extra local variable “st” hanging around. This can be annoying / dangerous in a long complex method.
[/quote]
another way to mimic With / End With while keeping the shortcut variable local could be using a For Each:
for each st as StyledText in array(Window1.TextArea1.StyledText)
st.Bold = True
st.Italic = True
st.Underline = True
next
but ok, the “if true” way seems to be easier to read
Actually I believe you are all overthinking this…
It is not a compiler issue as much as it is a pre-processor issue.
If done (what I would consider) properly. The compiler sees exactly what it sees today.
YOU type
with xyz
.property="fred"
.count=9
.abc=47
end with
The pre-processor changes it… to
xyz .property="fred"
xyz.count=9
xyz.abc=47
which is exactly what you would have to type today.
No CPU cycles… no extra memory allocations.
The “WITH” keyword simply “remembers” the object named… and any property starting with a period gets that object pre-pended, until then END WITH
Sure there are changes to the IDE, etc… but the resultant compiled application would be the same in both cases.
At least the compilers I have written produce the same code