Scope & Structs

Salih Dincer salihdb at hotmail.com
Sat Oct 12 12:02:04 UTC 2024


I have a small program like below. Everything works as it should 
in classes; even if I call the structure with the new operator. 
But if I stop using classes, scope doesn't work properly!

```d
class/* STEP2
struct//*/
Foo {
   this(int i) {
     i.writefln!"Object %s is created...";
   }
   ~this() {
   writeln("Object was deleted!");
   }
}

import std.stdio;
void main() {
    write("call ");
    writeln("return ", loop);
}

auto loop() {
   enum func = __FUNCTION__;
   func.writeln;

   for (auto i = 1; i != 3; ++i) {
     scope // STEP1
     auto foo = new Foo(i);
   }
   return func;
}

```

Please put a comment (//) mark at the beginning of the line that 
says STEP1 and then STEP2. Then change STEP1 back to its previous 
state, that is, enable the scope. You will sense that something 
is wrong...

Why doesn't it work correctly in structs?
Or is scope the default in structs?

SDB at 79


More information about the Digitalmars-d-learn mailing list