View previous topic :: View next topic |
Author |
Message |
Workaphobia
Joined: 24 Jun 2004 Posts: 17
|
Posted: Fri Sep 03, 2004 7:15 pm Post subject: Const attribute |
|
|
I've been wondering for quite some time why D lacks a true const qualifier (as in C++) for values that cannot be computed at compile time but are guaranteed to not change after initialization. Is this something that has been intentionally left out of the language design by Walter? Is it too difficult to implement given the other features? Or might it come in some future version of the language spec?
I heard some talk in Phobos Rising about creating both mutable and immutable versions of the same classes - is this the common solution used by anyone who prefers const-like type checking, or is there a better way? Would I be wise in D to just forget about constness altogether? _________________ "Nifty News Fifty: When news breaks, we give you the pieces." |
|
Back to top |
|
|
sean
Joined: 24 Jun 2004 Posts: 609 Location: Bay Area, CA
|
Posted: Fri Sep 03, 2004 7:25 pm Post subject: |
|
|
It was intentionally left out by Walter. Enforcing const behavior is difficult and inexact, and IMO Walter isn't the type to invest a lot of effort into something he sees as an imperfect solution. And as much as I do like the idea of const classes, it would mean const qualifiers everywhere, a mutable keyword, etc. It's easier to just copy a class in the instances where no side effects are desired. |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Fri Sep 03, 2004 10:06 pm Post subject: Re: Const attribute |
|
|
Workaphobia wrote: | I've been wondering for quite some time why D lacks a true const qualifier (as in C++) for values that cannot be computed at compile time but are guaranteed to not change after initialization. Is this something that has been intentionally left out of the language design by Walter? Is it too difficult to implement given the other features? Or might it come in some future version of the language spec?
I heard some talk in Phobos Rising about creating both mutable and immutable versions of the same classes - is this the common solution used by anyone who prefers const-like type checking, or is there a better way? Would I be wise in D to just forget about constness altogether? |
It is a bit of a hole in the spec; and will certainly keep the language out of the embedded market for now. Hopefully, that'll be remedied by the time there's real interest in that segment. There's a difference between the C++ const and the "place this in ROM" const.
There is actually a rather serious conflict between const, and struct arguments: to pass a struct by reference, you have to declare the parameter as out or inout ... obviously that does not fly for a const struct The only solution is to pass structs by value (egad!) or remove the const. Mango opted for the latter.
As to the mutable/immutable class split: that's not a const issue per se, although one could certainly look at it from that perspective. It's more a case of how to handle some nasty issues within multi-threaded and/or distributed environments. If one knows certain content cannot change, one can make all kind of useful assertions. The example often cited is with regard to caching distributed data. But benefits can often be reaped within any application (why Java strings are immutable).
Engineering such notions into a library doesn't cost anything other than a bit of thought, and exposes more strongly typed entities for the programmer to use or abuse
Mango takes this approach, partly because it is server-oriented. But I think it's to everyone's benefit to do something similar where it makes sense.
- Kris |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|