Implicit cast to const of result returned from findSplit()

Jacob Carlborg doob at me.com
Tue Nov 6 10:19:42 UTC 2018


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


More information about the Digitalmars-d-learn mailing list