www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implicit cast to const of result returned from findSplit()

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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?
Nov 05 2018
next sibling parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
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; } // ... }
Nov 05 2018
prev sibling next sibling parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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
Nov 05 2018
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
Nov 06 2018