Thread overview
Scope and with
Jan 23, 2013
SaltySugar
Jan 23, 2013
Namespace
Jan 23, 2013
simendsjo
Jan 23, 2013
Namespace
Jan 23, 2013
simendsjo
Jan 23, 2013
mist
Jan 23, 2013
simendsjo
Jan 23, 2013
Ali Çehreli
Jan 23, 2013
Ali Çehreli
January 23, 2013
Can someone explain me scope and with statements?
January 23, 2013
On Wednesday, 23 January 2013 at 08:33:44 UTC, SaltySugar wrote:
> Can someone explain me scope and with statements?

with statements simplify the access to a class, struct or something else.
For example:
class A {
public:
	int b = 42;
}

A a = new A();
normal access:
writeln(a.b); // writes 42

with 'with':

with (a) {
    writeln(b); // writes 42
}

scope is one of my favorite features. You can create scopes which are executed if the appropriate case comes.
For example at the end of a scope, by success of a scope etc.

Example:
int* mem = cast(int*) malloc(10 * int.sizeof);
scope(exit) free(mem);

After leaving scope the memory is freed.
Otherwise you have to write 'free(mem)' at the end of the scope and thats a thing that I forget sometimes in C.
January 23, 2013
On Wednesday, 23 January 2013 at 09:52:40 UTC, Namespace wrote:
> On Wednesday, 23 January 2013 at 08:33:44 UTC, SaltySugar wrote:
>> Can someone explain me scope and with statements?
>
> with statements simplify the access to a class, struct or something else.
> For example:
> class A {
> public:
> 	int b = 42;
> }
>
> A a = new A();
> normal access:
> writeln(a.b); // writes 42
>
> with 'with':
>
> with (a) {
>     writeln(b); // writes 42
> }
>
> scope is one of my favorite features. You can create scopes which are executed if the appropriate case comes.
> For example at the end of a scope, by success of a scope etc.
>
> Example:
> int* mem = cast(int*) malloc(10 * int.sizeof);
> scope(exit) free(mem);
>
> After leaving scope the memory is freed.
> Otherwise you have to write 'free(mem)' at the end of the scope and thats a thing that I forget sometimes in C.


I often use with to initialize objects (as D doesn't have object initializers as c#):

auto c = new C(); // some class
with(c) {
  someProp = someValue; // instead of c.someProp
  // etc
}

scope also has a different meaning if used in function parameters:
void f(scope C c) {
  // not allowed to pass c anywhere - it's not allowed to leave this scope
}
January 23, 2013
But AFAIK scope isn't fully implemented as storage class, or am I wrong?
January 23, 2013
On Wednesday, 23 January 2013 at 10:30:08 UTC, Namespace wrote:
> But AFAIK scope isn't fully implemented as storage class, or am I wrong?

I think you are right. And I think it's the reason using 'in' parameters are discouraged.
January 23, 2013
On Wednesday, 23 January 2013 at 10:43:24 UTC, simendsjo wrote:
> On Wednesday, 23 January 2013 at 10:30:08 UTC, Namespace wrote:
>> But AFAIK scope isn't fully implemented as storage class, or am I wrong?
>
> I think you are right. And I think it's the reason using 'in' parameters are discouraged.

I remember Kenji telling "in" currently is synonym for "const", not "const scope" as is often told.
January 23, 2013
On Wednesday, 23 January 2013 at 13:43:58 UTC, mist wrote:
> On Wednesday, 23 January 2013 at 10:43:24 UTC, simendsjo wrote:
>> On Wednesday, 23 January 2013 at 10:30:08 UTC, Namespace wrote:
>>> But AFAIK scope isn't fully implemented as storage class, or am I wrong?
>>
>> I think you are right. And I think it's the reason using 'in' parameters are discouraged.
>
> I remember Kenji telling "in" currently is synonym for "const", not "const scope" as is often told.

Stuff like this should really be in the documentation.. The docs says "in" is equivalent to "const scope", but doesn't mention this is not yet implemented and is just "const" for now..
January 23, 2013
On 01/23/2013 08:19 AM, simendsjo wrote:

> Stuff like this should really be in the documentation.. The docs says
> "in" is equivalent to "const scope",

That is correct.

> but doesn't mention this is not yet
> implemented and is just "const" for now..

I am not sure whether that is true for all compilers. The problem is, the language spec and dmd's implementation have been intertwined. That's our life... :)

Ali

January 23, 2013
On 01/23/2013 01:52 AM, Namespace wrote:

> int* mem = cast(int*) malloc(10 * int.sizeof);
> scope(exit) free(mem);
>
> After leaving scope the memory is freed.
> Otherwise you have to write 'free(mem)' at the end of the scope and
> thats a thing that I forget sometimes in C.

Yes, the correctness of code in C depends on remembering to do so and also being disciplined to follow a certain function design style. (All resources are released in the 'finally' area of the function.)

On the other hand, putting free(mem) at the end of a scope is a coding error in any language that has exceptions. (The exceptions of this rule are special lines of code where no exception can be thrown.) That's why RAII is an essential idiom in C++. 'scope' is a welcome addition to D that obviates the need for little RAII classes in some cases.

To the OP, here is a chapter on scope:

  http://ddili.org/ders/d.en/scope.html

That chapter contains references to the previous chapter, which is about exceptions:

  http://ddili.org/ders/d.en/exceptions.html

Ali