Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 23, 2014 Does remove immutability using cast to pass in a function make sense? | ||||
---|---|---|---|---|
| ||||
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 |
September 23, 2014 Re: Does remove immutability using cast to pass in a function make sense? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | 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
|
September 23, 2014 Re: Does remove immutability using cast to pass in a function make sense? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AsmMan | 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
|
September 24, 2014 Re: Does remove immutability using cast to pass in a function make sense? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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. :/
|
Copyright © 1999-2021 by the D Language Foundation