 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
toralf
Joined: 31 Jan 2005 Posts: 3910 Location: Bremen, Germany
|
Posted: Fri May 11, 2007 8:57 am Post subject: initialize static var with concatenated strings |
|
|
| Code: | test()
Return
test(){
static var := "string1"
. "string2"
Msgbox, %var%
} | I'm not sure if this is a bug or by design. Since initialization of static vars can be done with ":=" and "=" it seems that AHK only removes the outer """ and takes the rest as a normal string.
Since in my real function the static var needs to be initialized with several long strings I would like to concatenate them. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6836 Location: France (near Paris)
|
Posted: Fri May 11, 2007 9:30 am Post subject: |
|
|
That's indeed a strange bug.
I tried a workaround, but AHK complains: "Unsupported static initializer: string1string2"
| Code: | test()
Return
test(){
static var =
(Join
string1
string2
)
Msgbox, %var%
}
|
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10716
|
Posted: Fri May 11, 2007 10:20 am Post subject: |
|
|
It's documented that for strings, only literal/quoted strings such as "fox" are supported. I think this implies pretty strongly that expressions of any kind aren't supported.
Someday I hope expressions will be supported; but this requires that the program collect all static initializers, store their line numbers in memory somewhere, then run them one by one prior to the auto-execute section. There are other complications too. |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6836 Location: France (near Paris)
|
Posted: Fri May 11, 2007 10:53 am Post subject: |
|
|
Continuation sections aren't expressions, are they? _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10716
|
Posted: Fri May 11, 2007 1:31 pm Post subject: |
|
|
| You're right, I just realized they aren't supported. Hopefully they'll get supported automatically when support for expressions is added. |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Jan 21, 2010 12:19 am Post subject: |
|
|
Note: This is a semi-double post, I found this topic after I already replied to another topic (Re: Error message on static variable declaration) (search didn't return this one with my 1st terms), feel free to delete the other one or link it here...(& delete this note)...
I just wanted to add, that I just got that error when trying to use a continuation section to init a static...(didn't know I couldn't, {but I searched & found this topic})...
| Code: | testing() {
static example=
(LTrim
testing
)
} |
...a continuation section is a literal string, so if this is a special/easy case, I'd like to see it supported...but I understand if continuation sections are done later than static inits, making this difficult/impossible...just wanted to throw this case out there...
Expanding on the above example, none of them (global/static/local) "work"...
| Code: | testing() {
;// *** NOTE *** Test/Uncomment 1 at a time...
;//global example=
;//static example=
;//local example=
example=
(LTrim
testing
)
msgbox, msg(%example%)
}
testing() |
...when using global or local the msgbox is "msg()" (instead of "msg(testing)" {it's not getting the value from example}), using static you get that error ("Unsupported static initializer.")...so not even globals/locals "really" support this, even tho there's no error...
| Chris wrote: | | You're right, I just realized they aren't supported. |
...after testing all of the above, I tested this...it works...
| Code: | test_global() {
global g_Global="
(LTrim
value from global
)"
return g_Global
}
test_static() {
static s_Static="
(LTrim
value from static
)"
return s_Static
}
test_local() {
local l_Local="
(LTrim
value from local
)"
return l_Local
}
test_normal() {
normal=
(LTrim
value from normal =
)
return normal
}
test_expr() {
expr:="
(LTrim
value from expression :=
)"
return expr
}
ret_global:=test_global()
ret_static:=test_static()
ret_local:=test_local()
ret_normal:=test_normal()
ret_expr:=test_expr()
msgbox, 64, ,
(LTrim
test_global(%ret_global%)
test_static(%ret_static%)
test_local(%ret_local%)
test_normal(%ret_normal%)
test_expr(%ret_expr%)
) |
...so you CAN use continuation sections to init a global/static/local...but you need the double quotes positioned as in the code above... |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Thu Jan 21, 2010 9:46 am Post subject: |
|
|
| Anonymous wrote: | | ...a continuation section is a literal string, | Not necessarily. A continuation section is merely a way to merge multiple source lines. If used in an expression or where an expression is expected, it is still an expression.
| Quote: | | you CAN use continuation sections to init a global/static/local...but you need the double quotes positioned as in the code above... | You always need quotes in static var initializers; although they don't really support expressions, they use a subset of expression syntax. Global/local initializers require quotes only if you're assigning a literal string. It is equally valid to use a variable reference or function call, with or without a continuation section.
Actually, continuation sections are processed at such an early stage that they even work on directives and function parameter lists. The following two examples are equivalent:
| Code: | ErrorLevel := "Hello, world!"
F("Err","or","Level")
MsgBox % v
F(
( Join,
x
y
z
))
{
global v=
( LTrim Join
%x%
%y%
%z%
)
} |
| Code: | ErrorLevel := "Hello, world!"
F("Err","or","Level")
MsgBox % v
F(x,y,z)
{
global v=%x%%y%%z%
} |
|
|
| Back to top |
|
 |
Guest
|
Posted: Fri Jan 22, 2010 2:19 pm Post subject: |
|
|
| Lexikos wrote: | | ...it is still an expression. |
...OK, yes, I know that too (which is why I want an expression-less continuation section operator/option {merge this as a non-expression, passing it to the expression as a properly quoted string/whatever}, cuz trying to get them to behave within expressions is annoying...), but I meant in the case of "test_normal" it is a "literal string"...& I figured global/static/local was basically a test_normal case, but it's apparently not...I never knew "initializers" were so special...but I've also never had trouble with them until now...
| Lexikos wrote: | | You always need quotes in static var initializers... Global/local initializers require quotes only if you're assigning a literal string. |
...I've never had trouble with global/local & I must've never really used static this much or this way before...so I've either been doing it correctly...or accidentally avoiding it somehow...I didn't know either of them needed double quotes... |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 7295 Location: Australia
|
Posted: Mon Feb 01, 2010 10:06 am Post subject: |
|
|
| Quote: | | I never knew "initializers" were so special... | Static initializers are "special" because they must take effect only once, before the script begins executing. Local and global initializers are ordinary expressions, which is why they "require quotes only if you're assigning a literal string" (as I said earlier).
An advantage of the way continuation sections work with expressions:
| Code: | x := 2.468
MsgBox % "
(
Input: " x "
Round: " Round(x) "
Ceil: " Ceil(x) "
Floor: " Floor(x)
) | Notice that the continuation section inserts literal newline characters into the expression. (Specifying (Joindelimiter changes this.)
| Quote: | | merge this as a non-expression, passing it to the expression as a properly quoted string/whatever | I think that's a good idea (for optional behaviour). At its simplest, it would replace " with "" and put " at either end. But what about %vars% (which wouldn't be supported for static initializers anyway)?
Perhaps an option like:
| Code: | static var =
("
First line.
Second line.
) | However, it might be confusing given the current ways:
| Code: | static var1 = "
(
First line.
Second line.
)"
static var2 =
(
"First line.
Second line."
) | Maybe Q or Quote or QuotedString would be clearer. Then again, I suppose there's no ambiguity with:
| Code: | static var =
"
First line.
Second line.
" |
I think that would improve readability for function-calls containing continuation sections, like:
| Code: | LV_Add("",
"
First line of text.
Second line of text.
") | vs | Code: | LV_Add("",
(
"First line of text.
Second line of text."
))
|
|
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Tue Mar 16, 2010 3:41 am Post subject: |
|
|
Apologies for bumping an old thread, but continuation sections can be used with static initializers, for example:
| Code: | msgbox % Shipping_USStateCodes( "ca" )
Shipping_USStateCodes( code="DC" ) {
Static state_abbreviations := "
( ltrim join/
ALABAMA=AL
ALASKA=AK
AMERICAN SAMOA=AS
ARIZONA=AZ
ARKANSAS=AR
CALIFORNIA=CA
COLORADO=CO
CONNECTICUT=CT
DELAWARE=DE
DISTRICT OF COLUMBIA=DC
FEDERATED STATES OF MICRONESIA=FM
FLORIDA=FL
GEORGIA=GA
GUAM=GU
HAWAII=HI
IDAHO=ID
ILLINOIS=IL
INDIANA=IN
IOWA=IA
KANSAS=KS
KENTUCKY=KY
LOUISIANA=LA
MAINE=ME
MARSHALL ISLANDS=MH
MARYLAND=MD
MASSACHUSETTS=MA
MICHIGAN=MI
MINNESOTA=MN
MISSISSIPPI=MS
MISSOURI=MO
MONTANA=MT
NEBRASKA=NE
NEVADA=NV
NEW HAMPSHIRE=NH
NEW JERSEY=NJ
NEW MEXICO=NM
NEW YORK=NY
NORTH CAROLINA=NC
NORTH DAKOTA=ND
NORTHERN MARIANA ISLANDS=MP
OHIO=OH
OKLAHOMA=OK
OREGON=OR
PALAU=PW
PENNSYLVANIA=PA
PUERTO RICO=PR
RHODE ISLAND=RI
SOUTH CAROLINA=SC
SOUTH DAKOTA=SD
TENNESSEE=TN
TEXAS=TX
UTAH=UT
VERMONT=VT
VIRGIN ISLANDS=VI
VIRGINIA=VA
WASHINGTON=WA
WEST VIRGINIA=WV
WISCONSIN=WI
WYOMING=WY
)"
StringUpper, code, code
If RegexMatch( "/" state_abbreviations "/", "/\K[^=]*(?==\Q" code "\E/)", state )
StringUpper, state, state, T
Else RegexMatch( "/" state_abbreviations "/", "/\Q" code "\E=\K.{2}", state )
Return state
} |
Admittedly, the quotes (and double-quotes-for-one-literal-quote) are very reader-unfriendly, but I'm merely demonstrating that it's possible to initialize a static variable with a continuation section.
[Edit: RE:next post] Sorry, I missed that, guest. I suppose it doesn't hurt to have one more example for others' reference though... _________________ Ternary (a ? b : c) guide TSV Table Manipulation Library
Post code inside [code][/code] tags!
Last edited by [VxE] on Tue Mar 16, 2010 5:55 am; edited 1 time in total |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Mar 16, 2010 3:58 am Post subject: |
|
|
| [VxE] wrote: | | ...but continuation sections can be used with static initializers... |
...yes, I mentioned that...
| Anonymous wrote: | | ...so you CAN use continuation sections to init a global/static/local...but you need the double quotes positioned as in the code above... |
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|