Shall I use immutable or const while passing parameters to functions

jkpl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 9 22:15:52 PDT 2015


On Tuesday, 7 April 2015 at 15:11:39 UTC, tcak wrote:
> I have data in memory, and I want a function to take a part of 
> data for processing only. It will only read and won't change.
>
> char[] importantData;
>
>
> With Immutable,
>
> void dataProcessor( string giveMeAllYourData ){}
>
> dataProcessor( cast( immutable )( importantData[5 .. 14] ) );
>
>
>
> With Const,
>
> void dataProcessor( in char[] giveMeAllYourData ){}
>
> dataProcessor( cast( const )( importantData[5 .. 14] ) );
>
>
> (Code with const wasn't tested)
>
> Which one is better? I didn't understand it very well in 
> http://dlang.org/const3.html this page.

With `Object` or `struct` parameters, the `const` storage class 
is often a cripple because of its transitiveness. At least for me 
this is in the top five reasons why sometimes a program doesn't 
compile (with the error...`cannot modify const this`...)


More information about the Digitalmars-d-learn mailing list