www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const?

reply ZZ <ZZ zz.com> writes:
Why isn't it in D?
Does any one know why?

Zz
Feb 13 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
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
parent "Walter Bright" <newshound digitalmars.com> writes:
"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
prev sibling next sibling parent Derek Parnell <derek psych.ward> writes:
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
prev sibling parent reply "Chris Miller" <chris dprogramming.com> writes:
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?

 Zz
http://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
parent reply nick <nick.atamas gmail.com> writes:
Chris Miller wrote:
 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?

 Zz
http://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
If you have a class A, how do you make a const instance of it?
Feb 13 2006
next sibling parent reply Sean Kelly <sean f4.ca> writes:
nick wrote:
 Chris Miller wrote:
 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?

 Zz
http://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
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. Sean
Feb 13 2006
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
 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.
:-))) can I ask for something more natural, no?
Feb 13 2006
prev sibling next sibling parent "Chris Miller" <chris dprogramming.com> writes:
On Mon, 13 Feb 2006 21:58:04 -0500, nick <nick.atamas gmail.com> wrote:

 Chris Miller wrote:
 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?

 Zz
http://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
If you have a class A, how do you make a const instance of it?
Can only have const reference and assign an instance, const A a; static this() { a = new A; }
Feb 13 2006
prev sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
"nick" <nick.atamas gmail.com> wrote in message 
news:dsrgvg$hip$1 digitaldaemon.com...
 Chris Miller wrote:
 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?

 Zz
http://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
If 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. Or you can consider Ruby way and implement frozen object state. Andrew.
Feb 13 2006
next sibling parent nick <nick.atamas gmail.com> writes:
Andrew Fedoniouk wrote:
 "nick" <nick.atamas gmail.com> wrote in message 
 news:dsrgvg$hip$1 digitaldaemon.com...
 Chris Miller wrote:
 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?

 Zz
http://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
If 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. Or you can consider Ruby way and implement frozen object state. Andrew.
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...
Feb 13 2006
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 13 Feb 2006 19:17:30 -0800, Andrew Fedoniouk wrote:

 If 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.
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 PM
Feb 13 2006
next sibling parent reply "Walter Bright" <newshound digitalmars.com> writes:
"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
parent reply Derek Parnell <derek psych.ward> writes:
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...
 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.
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 PM
Feb 13 2006
parent "Walter Bright" <newshound digitalmars.com> writes:
"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
prev sibling next sibling parent reply Dave <Dave_member pathlink.com> writes:
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
parent Dave <Dave_member pathlink.com> writes:
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 issue
That 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
prev sibling parent Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
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