digitalmars.D.learn - const parameter passing?
- Serg Kovrov <Serg_member pathlink.com> Jan 25 2006
- Sean Kelly <sean f4.ca> Jan 25 2006
- Oskar Linde <oskar.lindeREM OVEgmail.com> Jan 25 2006
- "Craig Black" <cblack ara.com> Jan 25 2006
- nick <nick.atamas gmail.com> Jan 29 2006
- Serg Kovrov <dyh pathlink.com> Jan 31 2006
- Derek Parnell <derek psych.ward> Jan 31 2006
- Carlos Santander <csantander619 gmail.com> Jan 31 2006
Hi All,
Is there any way to prevent modification of object passed into function?
In C++ we have to do something like this:
void test(const Test& t)
{
int i = t.i; // ok
t.i = 0; // compile time error
// or
i = t.get(); // ok
t.set(0); // compile time error
}
But in D there is no 'const' function call parameter. Unfortunately 'in'
parameter didn't do the job.
ps. Sorry for possible duplicate of question - this newsgroup's search is really
bad =(
--serg
Jan 25 2006
Serg Kovrov wrote:Hi All, Is there any way to prevent modification of object passed into function?
No. To achieve this in D you would have to pass a copy of the object to the function. D has no concept of logical const-ness. Sean
Jan 25 2006
Sean Kelly wrote:Serg Kovrov wrote:Hi All, Is there any way to prevent modification of object passed into function?
No. To achieve this in D you would have to pass a copy of the object to the function. D has no concept of logical const-ness.
You could also possibly pass the object through a read-only interface. /Oskar
Jan 25 2006
Perhaps someone good with D templates could come up with a Const template that would provide read only access to an object. -Craig
Jan 25 2006
Serg Kovrov wrote:Hi All, Is there any way to prevent modification of object passed into function? In C++ we have to do something like this: void test(const Test& t) { int i = t.i; // ok t.i = 0; // compile time error // or i = t.get(); // ok t.set(0); // compile time error } But in D there is no 'const' function call parameter. Unfortunately 'in' parameter didn't do the job. ps. Sorry for possible duplicate of question - this newsgroup's search is really bad =( --serg
like lint.
Jan 29 2006
nick wrote:Serg Kovrov wrote:Is there any way to prevent modification of object passed into function? In C++ we have to do something like this: void test(const Test& t) { int i = t.i; // ok t.i = 0; // compile time error // or i = t.get(); // ok t.set(0); // compile time error } But in D there is no 'const' function call parameter. Unfortunately 'in' parameter didn't do the job.
I think D's philosophy is that this sort of thing is best left to tools like lint.
Thanks, I just curious what this 'lint' thing is, where to get it? But anyway, use of external tools for that purpose is not solution for me. -- serg.
Jan 31 2006
On Tue, 31 Jan 2006 23:30:49 +0200, Serg Kovrov wrote:nick wrote:Serg Kovrov wrote:Is there any way to prevent modification of object passed into function? In C++ we have to do something like this: void test(const Test& t) { int i = t.i; // ok t.i = 0; // compile time error // or i = t.get(); // ok t.set(0); // compile time error } But in D there is no 'const' function call parameter. Unfortunately 'in' parameter didn't do the job.
I think D's philosophy is that this sort of thing is best left to tools like lint.
Thanks, I just curious what this 'lint' thing is, where to get it? But anyway, use of external tools for that purpose is not solution for me.
'lint' is a utility program for C and C++ programs. It does a very thorough analysis of your code and warns you about things which the compiler does not report on. You can usually adjust the level of detail to be reported on. No one has written a 'lint' program for D yet, as far as I know. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 1/02/2006 10:00:12 AM
Jan 31 2006
Derek Parnell escribió:'lint' is a utility program for C and C++ programs. It does a very thorough analysis of your code and warns you about things which the compiler does not report on. You can usually adjust the level of detail to be reported on. No one has written a 'lint' program for D yet, as far as I know.
Didn't Ben write one? -- Carlos Santander Bernal
Jan 31 2006









Oskar Linde <oskar.lindeREM OVEgmail.com> 