AutoHotkey Community

It is currently May 27th, 2012, 5:40 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: May 11th, 2007, 9:57 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 11th, 2007, 10:30 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
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%
}

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 11th, 2007, 11:20 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 11th, 2007, 11:53 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Continuation sections aren't expressions, are they?

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 11th, 2007, 2:31 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
You're right, I just realized they aren't supported. Hopefully they'll get supported automatically when support for expressions is added.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2010, 1:19 am 
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...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 21st, 2010, 10:46 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
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%
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 22nd, 2010, 3:19 pm 
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...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 1st, 2010, 11:06 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
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."
))


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 16th, 2010, 4:41 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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 March 16th, 2010, 6:55 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 16th, 2010, 4:58 am 
[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...


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 6 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group