[Issue 20987] New: incompatible types for... when opBinary "in" overloading

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Jun 28 09:00:11 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=20987

          Issue ID: 20987
           Summary: incompatible types for... when opBinary "in"
                    overloading
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: vital.fadeev at gmail.com

Created attachment 1795
  --> https://issues.dlang.org/attachment.cgi?id=1795&action=edit
source code

The Source Code:

    import std.stdio;
    import std.algorithm.searching : find;


    struct Classes
    {
        string[] _classes;
        alias _classes this;


        auto opBinary( string op: "in" )( string rhs )
        {
            return _classes.find( rhs );
        }
    }


    void main()
    {
        Classes cs;

        cs ~= "box";
        writeln( cs._classes );

        auto res = "box" in cs._classes;
        writeln( res );
    }

The Goal:

   - Use overload operator "in"
   - Check string in string[]

Concrette:

   "box" in cs._classes;

Expected:

   Range r = "box" in cs._classes;
   assert( !r.empty );

Got:
   Compile time error:
       Error: incompatible types for ("box") in (cs._classes): string and
string[] 


Full log:
    C:\src\dtest-op-in>dub run
    Performing "debug" build using C:\D\dmd2\windows\bin\dmd.exe for x86_64.
    dtest-op-in ~master: building configuration "application"...
    source\app.d(25,16): Error: incompatible types for ("box") in
(cs._classes): string and string[]
    C:\D\dmd2\windows\bin\dmd.exe failed with exit code 1.

--


More information about the Digitalmars-d-bugs mailing list