Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 13, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZZ | 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
|
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZZ | 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 |
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ZZ | 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 |
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Miller | 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?
|
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to nick | 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
|
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to nick | 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;
}
|
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to nick | "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. |
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | >>
>> 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?
|
February 14, 2006 Re: const? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Fedoniouk | 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...
|
Copyright © 1999-2021 by the D Language Foundation