www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - IDEA: Use Unix-style chmod values for const and invariant

reply Russell Lewis <webmaster villagersonline.com> writes:
Ok, I was pondering, trying to figure out some sort of syntax which 
would allow us to communicate "const" and "invariant" in a way that's 
easy to learn & remember.  Maybe we could learn from UNIX file 
permissions, where you can see both the owner's permissions to the file, 
and "other's" permissions.  In that case, we have these comparisons:

const:		rw-r--r--
invariant:	r--r--r--

Which leads me to this concept for alternative keywords:

const:		const_wr
invariant:	const_rr

Sure, the first time that a C++ programmer sees this, he won't know 
exactly what it means, but it is (IMHO) easy to learn & remember.
Nov 29 2007
next sibling parent Kenny TM~ <kennytm gmail..com> writes:
Russell Lewis wrote:
 Ok, I was pondering, trying to figure out some sort of syntax which 
 would allow us to communicate "const" and "invariant" in a way that's 
 easy to learn & remember.  Maybe we could learn from UNIX file 
 permissions, where you can see both the owner's permissions to the file, 
 and "other's" permissions.  In that case, we have these comparisons:
 
 const:        rw-r--r--
 invariant:    r--r--r--
 
 Which leads me to this concept for alternative keywords:
 
 const:        const_wr
 invariant:    const_rr
 
 Sure, the first time that a C++ programmer sees this, he won't know 
 exactly what it means, but it is (IMHO) easy to learn & remember.

But it's hard to read IMHO. -- -- Kenny.
Nov 29 2007
prev sibling next sibling parent Chad J <gamerChad _spamIsBad_gmail.com> writes:
Russell Lewis wrote:
 Ok, I was pondering, trying to figure out some sort of syntax which 
 would allow us to communicate "const" and "invariant" in a way that's 
 easy to learn & remember.  Maybe we could learn from UNIX file 
 permissions, where you can see both the owner's permissions to the file, 
 and "other's" permissions.  In that case, we have these comparisons:
 
 const:        rw-r--r--
 invariant:    r--r--r--
 
 Which leads me to this concept for alternative keywords:
 
 const:        const_wr
 invariant:    const_rr
 
 Sure, the first time that a C++ programmer sees this, he won't know 
 exactly what it means, but it is (IMHO) easy to learn & remember.

I think I like where this is going. Of course, I like any attempt to generalize const into something more useful and maybe not so horribly named. I am having a bit of a problem making sure I understand the analogy though. So who is the "owner"? I'm guessing it's the variable being declared. Then who is "user" and "group"? I'm guessing those are subsumed into one entity who is other variables that reference this data. I suppose 'w' means read-write, not write only. Also, 'r' means read-only. The one on the left is the owner/decl, the one on the right is the user/group/others. Now just drop the horribly misleading term "const". Also, the underscore doesn't jive with me. const -> accesswr invariant -> accessrr Perhaps better can be done than "access". Maybe we can resurrect the notion of head and tail constness. const -> tailwr invariant -> tailrr More rambling (by me) is possible as well. ;)
Nov 29 2007
prev sibling parent reply Jesse Phillips <jessekphillips gmail.com> writes:
On Thu, 29 Nov 2007 10:32:56 -0700, Russell Lewis wrote:

 Ok, I was pondering, trying to figure out some sort of syntax which
 would allow us to communicate "const" and "invariant" in a way that's
 easy to learn & remember.  Maybe we could learn from UNIX file
 permissions, where you can see both the owner's permissions to the file,
 and "other's" permissions.  In that case, we have these comparisons:
 
 const:		rw-r--r--
 invariant:	r--r--r--
 
 Which leads me to this concept for alternative keywords:
 
 const:		const_wr
 invariant:	const_rr
 
 Sure, the first time that a C++ programmer sees this, he won't know
 exactly what it means, but it is (IMHO) easy to learn & remember.

Looking at this, I like the way it can be used to explain what they are, but I don't like the suggestion for new syntax. In my opinion invariant is a great keyword for what it does, don't ever change. But const like others is very much not descriptive of its use. I would think the two keywords should be, readonly ## which since keywords aren't usually 2, ro? invariant I'm not saying a name change is needed but maybe it should be looked into?
Nov 29 2007
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
On Nov 29, 2007 10:51 PM, Jesse Phillips <jessekphillips gmail.com> wrote:
 In my opinion invariant
 is a great keyword for what it does, don't ever change. But const like
 others is very much not descriptive of its use. I would think the two
 keywords should be,

 readonly ## which since keywords aren't usually 2, ro?
 invariant

 I'm not saying a name change is needed but maybe it should be looked into?

Has anyone thought of using "in" for readonly view? Keywords don't come much shorter. Its existing use is about as useful as unary plus.
Nov 29 2007
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Janice Caron wrote:
 On Nov 29, 2007 10:51 PM, Jesse Phillips <jessekphillips gmail.com> wrote:
 In my opinion invariant
 is a great keyword for what it does, don't ever change. But const like
 others is very much not descriptive of its use. I would think the two
 keywords should be,

 readonly ## which since keywords aren't usually 2, ro?
 invariant

 I'm not saying a name change is needed but maybe it should be looked into?

Has anyone thought of using "in" for readonly view? Keywords don't come much shorter. Its existing use is about as useful as unary plus.

Or how about 'is'? Or 'do'? Those are pretty short too! I think it hasn't been suggested because it doesn't jump out at anyone as meaning 'const' anywhere besides on a function parameter. --bb
Nov 29 2007
prev sibling parent reply Regan Heath <regan netmail.co.nz> writes:
Janice Caron wrote:
 On Nov 29, 2007 10:51 PM, Jesse Phillips <jessekphillips gmail.com> wrote:
 In my opinion invariant
 is a great keyword for what it does, don't ever change. But const like
 others is very much not descriptive of its use. I would think the two
 keywords should be,

 readonly ## which since keywords aren't usually 2, ro?
 invariant

 I'm not saying a name change is needed but maybe it should be looked into?

Has anyone thought of using "in" for readonly view? Keywords don't come much shorter. Its existing use is about as useful as unary plus.

We already use it for function parameters... http://www.digitalmars.com/d/function.html "The in storage class is equivalent to final const scope" :) R
Nov 30 2007
parent Regan Heath <regan netmail.co.nz> writes:
Janice Caron wrote:
 On Nov 30, 2007 9:16 AM, Regan Heath <regan netmail.co.nz> wrote:
 Has anyone thought of using "in" for readonly view? Keywords don't
 come much shorter. Its existing use is about as useful as unary plus.

http://www.digitalmars.com/d/function.html "The in storage class is equivalent to final const scope"

And yet, this compiles and runs in D2.0 int f(in int x) { ++x; return x; } void main() { int n = 42; int m = f(n); } It doesn't seem to mean const to me. (Anyway, "final" has been ditched).

So, the questions become: 1. does "in" still mean "const scope" (final being ditched) 2. if #1, does "const scope int x" mean that ++x should fail To answer #1 someone would have to dig round in the DMD front end I suspect. Or get an answer from Walter answer. Or, code two functions one using "in" and the other "const scope" and see if the behaviour is identical. To answer #2 ... Careful reading of Walters post about const, also what happens when you try the above with a class reference instead of an int? I haven't taken the time to really get inside the new system yet, which is why I'm asking the questions instead of answering them. Regan
Nov 30 2007
prev sibling next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On Nov 30, 2007 9:16 AM, Regan Heath <regan netmail.co.nz> wrote:
 Has anyone thought of using "in" for readonly view? Keywords don't
 come much shorter. Its existing use is about as useful as unary plus.

We already use it for function parameters... http://www.digitalmars.com/d/function.html "The in storage class is equivalent to final const scope"

And yet, this compiles and runs in D2.0 int f(in int x) { ++x; return x; } void main() { int n = 42; int m = f(n); } It doesn't seem to mean const to me. (Anyway, "final" has been ditched).
Nov 30 2007
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On Nov 30, 2007 9:56 AM, Janice Caron <caron800 googlemail.com> wrote:
 And yet, this compiles and runs in D2.0

Nov 30 2007