Thread overview
It there a way to get this to compile?
Sep 09, 2019
WhatMeWorry
Sep 09, 2019
Adam D. Ruppe
Sep 09, 2019
WhatMeWorry
Sep 09, 2019
a11e99z
Sep 09, 2019
Ali Çehreli
Sep 09, 2019
Ali Çehreli
September 09, 2019
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
    }			
}
September 09, 2019
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

September 09, 2019
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";			
>     }			
> }

September 09, 2019
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

September 09, 2019
On 09/09/2019 01:59 PM, Ali Çehreli 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 module scope. Non-compile-time statements like for, switch, etc. cannot appear at module scope either.

Ali


September 09, 2019
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";