digitalmars.D - inconstant foo
- Kevin Bealer <Kevin_member pathlink.com> May 11 2004
- James McComb <alan jamesmccomb.id.au> May 11 2004
- J Anderson <REMOVEanderson badmama.com.au> May 11 2004
I was thinking about "const" and I realized: D already has a form of const.
"The absence of a write method means that the property is read-only."
class const_foo {
char[] msg; /+ mutable member +/
int key() { return m_key; }
private:
int m_key;
};
/+ non-const foo +/
class foo : const_foo {
int key(int k) { m_key = k };
};
Now, functions that take a const_foo will not have access to key. For those who
are about to say "this violates IS-a": Just imagine the adjective had the
opposite meaning; instead of "const foo" and "foo", think "readable foo" and
"readable writeable foo".
Kevin
May 11 2004
Kevin Bealer wrote:Now, functions that take a const_foo will not have access to key. For those who are about to say "this violates IS-a": Just imagine the adjective had the opposite meaning; instead of "const foo" and "foo", think "readable foo" and "readable writeable foo".
I think you mean "writeable readable foo". :) I "writeable readable foo" IS A "readable foo". James McComb
May 11 2004
Kevin Bealer wrote:I was thinking about "const" and I realized: D already has a form of const. "The absence of a write method means that the property is read-only." class const_foo { char[] msg; /+ mutable member +/ int key() { return m_key; } private: int m_key; }; /+ non-const foo +/ class foo : const_foo { int key(int k) { m_key = k }; }; Now, functions that take a const_foo will not have access to key. For those who are about to say "this violates IS-a": Just imagine the adjective had the opposite meaning; instead of "const foo" and "foo", think "readable foo" and "readable writeable foo". Kevin
//common module 1 template ConstT(T) //This would be in another module { struct Const { private int val; static Const opCall(T val) { Const t; t.value = val; return t; } int value() { return val; } } } alias ConstT!(int).Const const; //module 2 void test(const value) { int x = value.value; //You just can't change the variable (well at least not without force). } void main() { int a; test(const(a)); //Ok there's a small bit of extra typing -> if D only had automatic boxing.... } -- -Anderson: http://badmama.com.au/~anderson/
May 11 2004









James McComb <alan jamesmccomb.id.au> 