keywords "objconst" and "objimmutable" vs. const(Object) ref
Christopher the Magnificent
ultimatemacfanatic at gmail.com
Tue May 17 17:00:55 PDT 2011
Greetings all. First time poster here.
By way of introduction, my name is Christopher. I've been fascinated
with D for a few years now. My main programming language has been
Python, but I have used and studied many programming languages to some
extent. I love to design things, like houses, pipe organs, and even
computer languages.
What I like about D is that it seems to strike the right balance of
allowing fairly high level programming with static typing yet still
permitting access on several progressively deeper levels to the nuts and
bolts of the computer. I also love D's module system and its dynamic
arrays, to name just a few things.
Now down to business. I want to talk about const and immutable. There
are 5,256 posts in this list containing the text "const" so I just this
topic is discussed often.
We all know that object references are really implemented as pointers to
the object's data structures on the heap.
When we say "const Object a", we declare a constant variable pointing to
a constant data structure. Sometimes we need to declare a modifiable
variable pointing to a constant data structure.
I propose the syntax "objconst Object a" to declare modifiable reference
variable pointing to a const object data structure, and "objimmutable
Object A" for modifiable reference variable pointing to immutable object
data structure:
objimmutable Object a = cast(immutable) new Object();
objconst Object b = new Object();
b = a; // b is modifiable
// ILLEGAL: b references constant data structure
b.someVariable = newValue;
These could also be used in the parenthesized way:
objconst(Object) a;
objconst(Object)*[1] a_array;
a = new Object();
a_array[0] = &a;
*a_array[0] = new Object(); // change a to a different object
Now I understand that another syntax has been nominated to do this job
which is the const(Object)ref syntax. I dislike that syntax because
"const(Object)" means a constant variable referencing a constant data
structure, and "const(Object)ref" LOOKS like it should be a reference to
a variable of type "const(Object)" -- to me it suggests a variable
referencing a constant variable referencing a constant data structure.
"const(Object)ref" suggests TWO levels of pointers, but that is not the
meaning being ascribed thereunto.
I picked the keywords "objconst" and "objimmutable" to suggest that the
object data structure (hence the "obj" prefix) is immutable or const.
This was a compromise between brevity and descriptiveness. I suppose
they could also go "constobj" and "immutableobj".
Another reason why the objconst/objimmutable syntax is better than
"const(Object)ref" is that it requires fewer tokens to parse and to type
-- this makes it easier to read:
const(Object) ref a; // 5 tokens in the type
objconst Object a; // only 2 tokens
I submitted this idea to the bug tracking system as a feature request,
but I also want to hear what people have to say about it on here.
What do you think guys, Mr. Bright? And what do you like
better--"objconst" and "objimmutable" or "constobj" and "immutableobj"?
--Christopher
More information about the Digitalmars-d
mailing list