www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does remove immutability using cast to pass in a function make sense?

reply "AsmMan" <jckj33 gmail.com> writes:
I have this array:

static immutable string[] months = ["jan", "fev", ...];

I need to pass it into canFind(). But it doesn't works with 
immutables so I need to cast it like in canFind(cast(string[]) 
months, month) to work. There's a reason related to design why it 
doesn't work with immutables or a function just wasn't written?

What I want to know is if I'm doing something wrong in casting it 
to work. I know from C which such casts should be done carefully
Sep 22 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 09/22/2014 07:07 PM, AsmMan wrote:
 I have this array:

 static immutable string[] months = ["jan", "fev", ...];

 I need to pass it into canFind(). But it doesn't works with immutables
 so I need to cast it like in canFind(cast(string[]) months, month) to
 work. There's a reason related to design why it doesn't work with
 immutables or a function just wasn't written?

 What I want to know is if I'm doing something wrong in casting it to
 work. I know from C which such casts should be done carefully
What is the compiler version? The following works with dmd git head: import std.algorithm; static immutable string[] months = ["jan", "fev" ]; void main() { assert(months.canFind("jan")); } If it still doesn't work for you please show us a minimal program that demonstrates the problem. Ali
Sep 22 2014
parent "AsmMan" <jckj33 gmail.com> writes:
On Tuesday, 23 September 2014 at 03:34:22 UTC, Ali Çehreli wrote:

 If it still doesn't work for you please show us a minimal 
 program that demonstrates the problem.

 Ali
Ok, the case is the follow, I updated my dmd compiler some days ago (after my mono crashed and I lost some of D files, I posted on this forum) and I can't reproduce the error anymore. What I remember it was about a template error and when I removed the immutable keyword it compiled. I tried really, but it doesn't give any error anymore and your code compile. :/
Sep 23 2014
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 9/22/14 10:07 PM, AsmMan wrote:
 I have this array:

 static immutable string[] months = ["jan", "fev", ...];

 I need to pass it into canFind(). But it doesn't works with immutables
 so I need to cast it like in canFind(cast(string[]) months, month) to
 work. There's a reason related to design why it doesn't work with
 immutables or a function just wasn't written?

 What I want to know is if I'm doing something wrong in casting it to
 work. I know from C which such casts should be done carefully
Yes, casting should be used very sparingly, and only when you have no other choice. Ali suggests that the latest version of DMD on git handles this, but just an FYI, you do not need to cast to get a mutable array of strings: canFind(months[], month) should work. -Steve
Sep 23 2014
prev sibling parent "kiran kumari" <kiranfabzen gmail.com> writes:
On Tuesday, 23 September 2014 at 02:07:09 UTC, AsmMan wrote:
 I have this array:

 static immutable string[] months = ["jan", "fev", ...];

 I need to pass it into canFind(). But it doesn't works with 
 immutables so I need to cast it like in canFind(cast(string[]) 
 months, month) to work. There's a reason related to design why 
 it doesn't work with immutables or a function just wasn't 
 written?

 What I want to know is if I'm doing something wrong in casting 
 it to work. I know from C which such casts should be done 
 carefully
see more example http://techgurulab.com/course/java-quiz-online/
Sep 23 2014