Shall I use immutable or const while passing parameters to functions

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 7 10:26:23 PDT 2015


On Tuesday, 7 April 2015 at 15:51:59 UTC, bearophile wrote:
> 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

I am trying to avoid it as much as I can, though this "shared" 
keyword is not leaving me alone. Most of the projects I am 
working on use multiple threads, and I am mostly obliged to use 
casting many times. I am getting worried some times about what 
the compiler is doing when I do casting other than just changing 
a simple understanding like taking the first byte of ulong when I 
do casting, etc. Please do not suggesting to use __gshared. It is 
ugly looking.


More information about the Digitalmars-d-learn mailing list