Re: Shall I use immutable or const while passing parameters to functions

2015-04-07 Thread John Colvin via Digitalmars-d-learn

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.


const means I will not modify this via this variable and 
immutable means no one will modify this from anywhere


Don't cast to immutable unless you are sure what you are doing. 
Immutable is a strong guarantee, it's easy to break it by 
accident if you're casting things. The safest way of getting an 
immutable array is with the .idup property.


You don't have to cast to const most of the time. mutable is 
implicitly convertible to const:


void foo(const int[] a){}

void main()
{
int[] a = [1,2,3,4];
foo(a);
foo(a[0..2]);
}

Also, see https://archive.org/details/dconf2013-day01-talk02

In general, immutable in an API allows greater freedom for the 
implementation, const allows for greater freedom/convenience for 
the API user. Which is more important depends on your exact 
problem.


Re: Shall I use immutable or const while passing parameters to functions

2015-04-07 Thread Adam D. Ruppe via Digitalmars-d-learn
If you're just looking at the data, use const. immutable becomes 
more important if it is shared across threads or stored for later.


Functions that accept const will work with almost anything you 
pass to it.


Shall I use immutable or const while passing parameters to functions

2015-04-07 Thread tcak via Digitalmars-d-learn
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.


Re: Shall I use immutable or const while passing parameters to functions

2015-04-07 Thread bearophile via Digitalmars-d-learn

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


Re: Shall I use immutable or const while passing parameters to functions

2015-04-07 Thread tcak via Digitalmars-d-learn

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.