Re: Does remove immutability using cast to pass in a function make sense?

2014-09-24 Thread kiran kumari via Digitalmars-d-learn

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/


Re: Does remove immutability using cast to pass in a function make sense?

2014-09-23 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: Does remove immutability using cast to pass in a function make sense?

2014-09-23 Thread AsmMan via Digitalmars-d-learn

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. :/


Does remove immutability using cast to pass in a function make sense?

2014-09-22 Thread AsmMan via Digitalmars-d-learn

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


Re: Does remove immutability using cast to pass in a function make sense?

2014-09-22 Thread Ali Çehreli via Digitalmars-d-learn

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