www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Scope and with

reply "SaltySugar" <Butkustomas777 gmail.com> writes:
Can someone explain me scope and with statements?
Jan 23 2013
parent reply "Namespace" <rswhite4 googlemail.com> writes:
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.
Jan 23 2013
next sibling parent reply "simendsjo" <simendsjo gmail.com> writes:
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 }
Jan 23 2013
parent reply "Namespace" <rswhite4 googlemail.com> writes:
But AFAIK scope isn't fully implemented as storage class, or am I 
wrong?
Jan 23 2013
parent reply "simendsjo" <simendsjo gmail.com> writes:
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.
Jan 23 2013
parent reply "mist" <none none.none> writes:
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.
Jan 23 2013
parent reply "simendsjo" <simendsjo gmail.com> writes:
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..
Jan 23 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jan 23 2013
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jan 23 2013