Shall I use immutable or const while passing parameters to functions

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 7 08:51:58 PDT 2015


tcak:

> void dataProcessor( string giveMeAllYourData ){}
>
> dataProcessor( cast( immutable )( importantData[5 .. 14] ) );
>
>
>
> With Const,
>
> void dataProcessor( in char[] giveMeAllYourData ){}
>
> dataProcessor( cast( const )( importantData[5 .. 14] ) );

Don't cast to const/immutable unless you have a good reason to do 
it, and you know what you are doing (and most times you don't 
know it).
More generally, minimize the number of cast() in your D programs. 
You can use a search to count how many "cast(" there are in your 
whole D codebase, and you can try to reduce that number.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list