Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 05, 2018 Implicit cast to const of result returned from findSplit() | ||||
---|---|---|---|---|
| ||||
Why does @safe pure unittest { import std.algorithm.searching : findSplit; if (const split = "a b".findSplit(" ")) { } } error as f.d(4,5): Error: mutable method `std.algorithm.searching.findSplit!("a == b", string, string).findSplit.Result!(string, string).Result.opCast!bool.opCast` is not callable using a `const` object f.d(4,5): Consider adding `const` or `inout` to std.algorithm.searching.findSplit!("a == b", string, string).findSplit.Result!(string, string).Result.opCast!bool.opCast when @safe pure unittest { import std.algorithm.searching : findSplit; if (auto split = "a b".findSplit(" ")) { } } doesn't? AFAICT, it looks like a missing bool qualifier on `opCast!bool`, right? |
November 05, 2018 Re: Implicit cast to const of result returned from findSplit() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Monday, 5 November 2018 at 13:26:18 UTC, Per Nordlöw wrote:
> AFAICT, it looks like a missing bool qualifier on `opCast!bool`, right?
...Like a missing 'const' qualifier ;)
auto findSplit(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle)
// ...
static struct Result(S1, S2) if (isForwardRange!S1 &&
isForwardRange!S2)
{
// ...
bool opCast(T : bool)()
{
return !asTuple[1].empty;
}
// ...
}
|
November 05, 2018 Re: Implicit cast to const of result returned from findSplit() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Monday, 5 November 2018 at 13:26:18 UTC, Per Nordlöw wrote: > AFAICT, it looks like a missing bool qualifier on `opCast!bool`, right? Fixed at https://github.com/dlang/phobos/pull/6749 |
November 06, 2018 Re: Implicit cast to const of result returned from findSplit() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On 2018-11-05 14:26, Per Nordlöw wrote: > Why does > > @safe pure unittest > { > import std.algorithm.searching : findSplit; > if (const split = "a b".findSplit(" ")) > { > } > } > > error as > > f.d(4,5): Error: mutable method `std.algorithm.searching.findSplit!("a == b", string, string).findSplit.Result!(string, string).Result.opCast!bool.opCast` is not callable using a `const` object > f.d(4,5): Consider adding `const` or `inout` to std.algorithm.searching.findSplit!("a == b", string, string).findSplit.Result!(string, string).Result.opCast!bool.opCast > > when > > @safe pure unittest > { > import std.algorithm.searching : findSplit; > if (auto split = "a b".findSplit(" ")) > { > } > } > > doesn't? > > AFAICT, it looks like a missing bool qualifier on `opCast!bool`, right? If the first example you declare a const variable of the type that "findSplit" returns. Then the compiler will call opCast since the variable is defined in the if condition. But you can only call methods marked as "const" if you have a const variable. The opCast method in the struct returned by "findSplit" is missing a const attribute. -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation