Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
January 25, 2006 const parameter passing? | ||||
---|---|---|---|---|
| ||||
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 |
January 25, 2006 Re: const parameter passing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serg Kovrov | 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
|
January 25, 2006 Re: const parameter passing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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
|
January 25, 2006 Re: const parameter passing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serg Kovrov | Perhaps someone good with D templates could come up with a Const template that would provide read only access to an object. -Craig |
January 29, 2006 Re: const parameter passing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serg Kovrov | 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.
|
January 31, 2006 Re: const parameter passing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to nick | 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.
|
January 31, 2006 Re: const parameter passing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Serg Kovrov | 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 |
February 01, 2006 Re: const parameter passing? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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 |
Copyright © 1999-2021 by the D Language Foundation