digitalmars.D - const?
- ZZ (3/3) Feb 13 2006 Why isn't it in D?
- Sean Kelly (8/10) Feb 13 2006 Basically, because Walter isn't entirely happy with any existing
- Walter Bright (3/9) Feb 13 2006 LOL, that's a good summary of the situation.
- Derek Parnell (51/53) Feb 13 2006 Because it means many different things and some are easy to do, some har...
- Chris Miller (5/8) Feb 13 2006 http://www.digitalmars.com/d/attribute.html#const
- nick (2/14) Feb 13 2006 If you have a class A, how do you make a const instance of it?
- Sean Kelly (5/19) Feb 13 2006 I think the closest you could come in D would be to define a custom
- Andrew Fedoniouk (2/7) Feb 13 2006 :-)))
- Chris Miller (7/21) Feb 13 2006 Can only have const reference and assign an instance,
- Andrew Fedoniouk (14/28) Feb 13 2006 class A
- nick (4/42) Feb 13 2006 Can _I_ ask for something more natural? =)
- Derek Parnell (45/60) Feb 13 2006 Thanks Andrew,
- Walter Bright (6/13) Feb 13 2006 With casting and pointers, you'll *always* be able to subvert both the t...
- Derek Parnell (11/27) Feb 13 2006 I guess I didn't make it clear but I think that its a *good* thing that ...
- Walter Bright (4/8) Feb 13 2006 I agree, which fits in with D being a systems level language.
- Dave (22/64) Feb 14 2006 If you add a 'package specifier' to each module declaration then it will...
- Dave (2/6) Feb 14 2006
- Bruno Medeiros (9/25) Feb 14 2006 Not methods, but members. That is: methods and fields, but not inner
ZZ wrote:Why isn't it in D? Does any one know why?Basically, because Walter isn't entirely happy with any existing implementations (C++ being one such example) and is hoping a better solution comes to light. Feelings on this issue are mixed, because while most people seem to agree that the C++ method isn't ideal, many still want some checking in place. This issue competes with the bit/bool issue in being the most debated topic on this forum ;-) Sean
Feb 13 2006
"Sean Kelly" <sean f4.ca> wrote in message news:dsr5rm$82h$1 digitaldaemon.com...Basically, because Walter isn't entirely happy with any existing implementations (C++ being one such example) and is hoping a better solution comes to light. Feelings on this issue are mixed, because while most people seem to agree that the C++ method isn't ideal, many still want some checking in place. This issue competes with the bit/bool issue in being the most debated topic on this forum ;-)LOL, that's a good summary of the situation.
Feb 13 2006
On Tue, 14 Feb 2006 01:27:29 +0200, ZZ wrote:Why isn't it in D? Does any one know why?Because it means many different things and some are easy to do, some hard and some impossible without hardware/VM support. There is no agreement about what one means by it. Walter will address some of the 'const' issues in the (distant?) future but for now, there is no likelihood of anything like 'const' happening. For example, why not give us your meaning on const and how you see it helping your applications. I'm sure we will soon get somebody disagreeing with some aspects of your ideas :-) In my case, I do not won't 'const'-qualified data to be actively protected from change, but I'd like the "-w" switch to tell me about direct attempts to modify such data, and be quiet about indirect attempts to modify it. A the data I'm referring to is intrinsic datatypes (int, real, etc...) and that which is directly (one level deep) owned by the aggregate data structures (arrays, structs, and objects). For example: class Foo { char[10] A; char[] B; const char[] C; } const Foo f = new Foo; f.A[0] = 'x'; // bad 'cos all of A is owned by f. f.B.length = 10; // bad 'cos B is a reference and is owned by f. f.B[0] = 'x'; // Okay, 'cos the data B points to is not owned by f. f.C.length = 10; // bad 'cos C is a reference and is owned by f. f.C[0] = 'x'; // bad, 'cos the data C points to is owned by const C. // To allow updates to f access (f) { f.A[0] = 'x'; f.C.length = 10; access (f.C) { f.C[0] = 'x'; } } This enables the compiler to warn me about accidental updates but still allows me to make deliberate updates. But as you can imagine, there are lots of corner cases that need to be fleshed out and addressed. But the principle is still the same, I want to be alerted to accidental updates and I want to be able to make deliberate updates. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 14/02/2006 10:41:20 AM
Feb 13 2006
On Mon, 13 Feb 2006 18:27:29 -0500, ZZ <ZZ zz.com> wrote:Why isn't it in D? Does any one know why? Zzhttp://www.digitalmars.com/d/attribute.html#const If you mean "Why isn't D const like c/c++ const?" then searching around can find reasons; here's one, http://www.digitalmars.com/d/archives/15187.html
Feb 13 2006
Chris Miller wrote:On Mon, 13 Feb 2006 18:27:29 -0500, ZZ <ZZ zz.com> wrote:If you have a class A, how do you make a const instance of it?Why isn't it in D? Does any one know why? Zzhttp://www.digitalmars.com/d/attribute.html#const If you mean "Why isn't D const like c/c++ const?" then searching around can find reasons; here's one, http://www.digitalmars.com/d/archives/15187.html
Feb 13 2006
nick wrote:Chris Miller wrote:I think the closest you could come in D would be to define a custom allocator that places the class instance in the static data area, then rely on hardware access protection to flag any attempts at modification. SeanOn Mon, 13 Feb 2006 18:27:29 -0500, ZZ <ZZ zz.com> wrote:If you have a class A, how do you make a const instance of it?Why isn't it in D? Does any one know why? Zzhttp://www.digitalmars.com/d/attribute.html#const If you mean "Why isn't D const like c/c++ const?" then searching around can find reasons; here's one, http://www.digitalmars.com/d/archives/15187.html
Feb 13 2006
:-))) can I ask for something more natural, no?If you have a class A, how do you make a const instance of it?I think the closest you could come in D would be to define a custom allocator that places the class instance in the static data area, then rely on hardware access protection to flag any attempts at modification.
Feb 13 2006
On Mon, 13 Feb 2006 21:58:04 -0500, nick <nick.atamas gmail.com> wrote:Chris Miller wrote:Can only have const reference and assign an instance, const A a; static this() { a = new A; }On Mon, 13 Feb 2006 18:27:29 -0500, ZZ <ZZ zz.com> wrote:If you have a class A, how do you make a const instance of it?Why isn't it in D? Does any one know why? Zzhttp://www.digitalmars.com/d/attribute.html#const If you mean "Why isn't D const like c/c++ const?" then searching around can find reasons; here's one, http://www.digitalmars.com/d/archives/15187.html
Feb 13 2006
"nick" <nick.atamas gmail.com> wrote in message news:dsrgvg$hip$1 digitaldaemon.com...Chris Miller wrote:class A { int foo() { return _foo; } package this() {} package ~this(){} // data package int _foo; } Instances of this class are effectively read-only outside of your package and are managed by your package. Or you can consider Ruby way and implement frozen object state. Andrew.On Mon, 13 Feb 2006 18:27:29 -0500, ZZ <ZZ zz.com> wrote:If you have a class A, how do you make a const instance of it?Why isn't it in D? Does any one know why? Zzhttp://www.digitalmars.com/d/attribute.html#const If you mean "Why isn't D const like c/c++ const?" then searching around can find reasons; here's one, http://www.digitalmars.com/d/archives/15187.html
Feb 13 2006
Andrew Fedoniouk wrote:"nick" <nick.atamas gmail.com> wrote in message news:dsrgvg$hip$1 digitaldaemon.com...Can _I_ ask for something more natural? =) Seriously, I don't know if const is needed. As far as I can tell, Java has final and that seems to be enough...Chris Miller wrote:class A { int foo() { return _foo; } package this() {} package ~this(){} // data package int _foo; } Instances of this class are effectively read-only outside of your package and are managed by your package. Or you can consider Ruby way and implement frozen object state. Andrew.On Mon, 13 Feb 2006 18:27:29 -0500, ZZ <ZZ zz.com> wrote:If you have a class A, how do you make a const instance of it?Why isn't it in D? Does any one know why? Zzhttp://www.digitalmars.com/d/attribute.html#const If you mean "Why isn't D const like c/c++ const?" then searching around can find reasons; here's one, http://www.digitalmars.com/d/archives/15187.html
Feb 13 2006
On Mon, 13 Feb 2006 19:17:30 -0800, Andrew Fedoniouk wrote:Thanks Andrew, this is almost there for me too. I did some testing and it works for the most part. The part I can't seem to get working is that modules in the same package still can't access the 'package' members. Consider my test... ----- m1.d ---- import m2; void Bar() { Q q; q.Foo(); } ---- m2.d ---- import m1; struct Q { package void Foo() {} } ------------- Now I have these in the same directory and compile with "dmd m1 m2 -c" but get this message "m1.d(5): struct m2.Q member Foo is not accessible". However, if I make this change to m2.d ... ---- m2.d ---- import m1; package struct Q { void Foo() {} } ------------ now it compiles. But my understanding is that the 'package' modifier only applies if it is against a method name and not a class declaration. BTW, the 'package' modifier still let's me do nasty things if I really had to ... Q q; real *b; *(b = cast(real *)&q) = 3.14; This compiles even if 'package' or 'private' has been specified, thus allowing very low-level updating. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 14/02/2006 3:13:19 PMIf you have a class A, how do you make a const instance of it?class A { int foo() { return _foo; } package this() {} package ~this(){} // data package int _foo; } Instances of this class are effectively read-only outside of your package and are managed by your package.
Feb 13 2006
"Derek Parnell" <derek psych.ward> wrote in message news:oulocmdyp9w8.8tq0o500xmxx$.dlg 40tude.net...BTW, the 'package' modifier still let's me do nasty things if I really had to ... Q q; real *b; *(b = cast(real *)&q) = 3.14; This compiles even if 'package' or 'private' has been specified, thus allowing very low-level updating.With casting and pointers, you'll *always* be able to subvert both the type system and any const semantics (unless const data is protected by the hardware). The interesting question is whether such deliberate subversion invokes defined or undefined behavior.
Feb 13 2006
On Mon, 13 Feb 2006 21:54:37 -0800, Walter Bright wrote:"Derek Parnell" <derek psych.ward> wrote in message news:oulocmdyp9w8.8tq0o500xmxx$.dlg 40tude.net...I guess I didn't make it clear but I think that its a *good* thing that D allows this close-to-the-metal coding. There are going to be times when such anti-social coding is justified. So is the 'package' issue I also talked about a bug or expected behaviour? -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 14/02/2006 5:49:02 PMBTW, the 'package' modifier still let's me do nasty things if I really had to ... Q q; real *b; *(b = cast(real *)&q) = 3.14; This compiles even if 'package' or 'private' has been specified, thus allowing very low-level updating.With casting and pointers, you'll *always* be able to subvert both the type system and any const semantics (unless const data is protected by the hardware). The interesting question is whether such deliberate subversion invokes defined or undefined behavior.
Feb 13 2006
"Derek Parnell" <derek psych.ward> wrote in message news:1tpldiq9zop22.1ck8djgnrm2j2$.dlg 40tude.net...I guess I didn't make it clear but I think that its a *good* thing that D allows this close-to-the-metal coding. There are going to be times when such anti-social coding is justified.I agree, which fits in with D being a systems level language.So is the 'package' issue I also talked about a bug or expected behaviour?I'll have to study it, as it wasn't immediately obvious.
Feb 13 2006
In article <oulocmdyp9w8.8tq0o500xmxx$.dlg 40tude.net>, Derek Parnell says...this is almost there for me too. I did some testing and it works for the most part. The part I can't seem to get working is that modules in the same package still can't access the 'package' members. Consider my test... ----- m1.d ---- import m2; void Bar() { Q q; q.Foo(); } ---- m2.d ---- import m1; struct Q { package void Foo() {} } ------------- Now I have these in the same directory and compile with "dmd m1 m2 -c" but get this message "m1.d(5): struct m2.Q member Foo is not accessible".If you add a 'package specifier' to each module declaration then it will work, which is consistent with the docs: module test.m1; import m2; void Bar() { Q q; q.Foo(); } ;--- module test.m2; import m1; struct Q { package void Foo() {} }However, if I make this change to m2.d ... ---- m2.d ---- import m1; package struct Q { void Foo() {} } ------------ now it compiles. But my understanding is that the 'package' modifier only applies if it is against a method name and not a class declaration.'package' is ignored here (so is final). I think the only attribute that applies to an aggregate like that is 'final' for classes. That would explain the issue below too. This has caused quite a bit of confusion in the past - I wish the compiler would flag things like 'private class' because they are ignored and can be misleading.BTW, the 'package' modifier still let's me do nasty things if I really had to ... Q q; real *b; *(b = cast(real *)&q) = 3.14; This compiles even if 'package' or 'private' has been specified, thus allowing very low-level updating. Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 14/02/2006 3:13:19 PM
Feb 14 2006
In article <dssvnu$1u8a$1 digitaldaemon.com>, Dave says...'package' is ignored here (so is final). I think the only attribute that applies to an aggregate like that is 'final' for classes. That would explain the issueThat should read "so is private".This has caused quite a bit of confusion in the past - I wish the compiler would flag things like 'private class' because they are ignored and can be misleading.
Feb 14 2006
Derek Parnell wrote:------------ now it compiles. But my understanding is that the 'package' modifier only applies if it is against a method name and not a class declaration. BTW, the 'package' modifier still let's me do nasty things if I really had to ... Q q; real *b; *(b = cast(real *)&q) = 3.14; This compiles even if 'package' or 'private' has been specified, thus allowing very low-level updating.Not methods, but members. That is: methods and fields, but not inner declarations. I don't find this behavior desirable, I think protection attributes should work orthogonally to all entities in declaration blocks/scopes. -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Feb 14 2006