digitalmars.D - inconstant foo
- Kevin Bealer (17/17) May 11 2004 I was thinking about "const" and I realized: D already has a form of con...
- James McComb (4/8) May 11 2004 I think you mean "writeable readable foo". :)
- J Anderson (32/50) May 11 2004 I mentioned you could do something like this a few weeks ago:
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". KevinI mentioned you could do something like this a few weeks ago: //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