digitalmars.D.learn - Scope and with
- SaltySugar (1/1) Jan 23 2013 Can someone explain me scope and with statements?
- Namespace (24/25) Jan 23 2013 with statements simplify the access to a class, struct or
- simendsjo (13/38) Jan 23 2013 I often use with to initialize objects (as D doesn't have object
- Namespace (2/2) Jan 23 2013 But AFAIK scope isn't fully implemented as storage class, or am I
- simendsjo (3/5) Jan 23 2013 I think you are right. And I think it's the reason using 'in'
- mist (3/8) Jan 23 2013 I remember Kenji telling "in" currently is synonym for "const",
- simendsjo (4/13) Jan 23 2013 Stuff like this should really be in the documentation.. The docs
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/10) Jan 23 2013 I am not sure whether that is true for all compilers. The problem is,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (15/20) Jan 23 2013 Yes, the correctness of code in C depends on remembering to do so and
Can someone explain me scope and with statements?
Jan 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.
Jan 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:I often use with to initialize objects (as D doesn't have object 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 }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
But AFAIK scope isn't fully implemented as storage class, or am I wrong?
Jan 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.
Jan 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:I remember Kenji telling "in" currently is synonym for "const", not "const scope" as is often told.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
On Wednesday, 23 January 2013 at 13:43:58 UTC, mist wrote:On Wednesday, 23 January 2013 at 10:43:24 UTC, simendsjo wrote: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..On Wednesday, 23 January 2013 at 10:30:08 UTC, Namespace wrote:I remember Kenji telling "in" currently is synonym for "const", not "const scope" as is often told.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
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
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