www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - const parameter passing?

reply Serg Kovrov <Serg_member pathlink.com> writes:
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
next sibling parent reply Sean Kelly <sean f4.ca> writes:
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
parent Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
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
prev sibling next sibling parent "Craig Black" <cblack ara.com> writes:
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
prev sibling parent reply nick <nick.atamas gmail.com> writes:
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
I think D's philosophy is that this sort of thing is best left to tools like lint.
Jan 29 2006
parent reply Serg Kovrov <dyh pathlink.com> writes:
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
parent reply Derek Parnell <derek psych.ward> writes:
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
parent Carlos Santander <csantander619 gmail.com> writes:
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