www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - It there a way to get this to compile?

reply WhatMeWorry <kheaser gmail.com> writes:
Is this even possible?

klondike.d(155): Error: no identifier for declarator c
klondike.d(155): Error: declaration expected, not =

struct FoundationPile
{
     Card[] up;    // all cards are face up on the Foundation
}

FoundationPile[4] foundations;


static if(true)
{
     int r = 2;
     int c = 40;
     static foreach(i ; foundations)
     {
         immutable string brackets = "\033[" ~ to!string(r) ~ ";" 
~ to!string(c) ~ "H";			
         c = c + 10;   // line 155
     }			
}
Sep 09 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 9 September 2019 at 19:08:17 UTC, WhatMeWorry wrote:
 Is this even possible?
what are you trying to do? if c is static, it just needs to be initialized by a helper function, like int helper() { int c = 60; foreach(f; foundations) c += 10; return c; } static int c = helper(); // it initializes based on teh function now
Sep 09 2019
parent WhatMeWorry <kheaser gmail.com> writes:
On Monday, 9 September 2019 at 19:12:34 UTC, Adam D. Ruppe wrote:
 On Monday, 9 September 2019 at 19:08:17 UTC, WhatMeWorry wrote:
 Is this even possible?
what are you trying to do? if c is static, it just needs to be initialized by a helper function, like int helper() { int c = 60; foreach(f; foundations) c += 10; return c; } static int c = helper(); // it initializes based on teh function now
Oops. I'm trying to build up a complex formatting string of Console Virtual Terminal Sequences. Should be ~= or is that =~. I never can remember. immutable string brackets ~= "\033[" ~ to!string(r) ~ ";" ~ to!string(c) ~ "H";
Sep 09 2019
prev sibling next sibling parent a11e99z <black80 bk.ru> writes:
On Monday, 9 September 2019 at 19:08:17 UTC, WhatMeWorry wrote:
 struct FoundationPile {
     Card[] up;    // all cards are face up on the Foundation
 }

 FoundationPile[4] foundations;

 static if(true) {
     int r = 2;
     int c = 40;
     static foreach(i ; foundations) {
         immutable string brackets = "\033[" ~ to!string(r) ~ 
 ";" ~ to!string(c) ~ "H";			
         c = c + 10;   // line 155
     }			
 }
NB dont use name "i" for foreach/ranges, foreach can be used with index too, use "el" as "element" or "it" as "iterator" or just "something"
 static if(true) {
     enum r = 2, c = 40; // bad names too
     static foreach( idx, el; foundations) {
         immutable string brackets = "\033[" ~ to!string(r) ~ 
 ";" ~ to!string(c + idx*10) ~ "H";			
     }			
 }
Sep 09 2019
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 09/09/2019 12:08 PM, WhatMeWorry wrote:
 Is this even possible?
No because there can't be expressions at module scope.
 static if(true)
 {
      int r = 2;
      int c = 40;
Those are fine.
      static foreach(i ; foundations)
      {
          immutable string brackets = "\033[" ~ to!string(r) ~ ";" ~
 to!string(c) ~ "H";
That's fine as well.
          c = c + 10;   // line 155
That expression cannot appear at module scope. (In case it's not clear, 'static if' and 'static foreach' disappear after a stage of compilation and their contents are left behind at module scope.) Ali
Sep 09 2019
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 09/09/2019 01:59 PM, Ali =C3=87ehreli wrote:
 On 09/09/2019 12:08 PM, WhatMeWorry wrote:
  > Is this even possible?

 No because there can't be expressions at module scope.
Attempting to correct myself: I think only declarations are allowed at=20 module scope. Non-compile-time statements like for, switch, etc. cannot=20 appear at module scope either. Ali
Sep 09 2019