D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20987 - incompatible types for... when opBinary "in" overloading
Summary: incompatible types for... when opBinary "in" overloading
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-28 09:00 UTC by Vital
Modified: 2020-06-28 12:09 UTC (History)
2 users (show)

See Also:


Attachments
source code (364 bytes, text/plain)
2020-06-28 09:00 UTC, Vital
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Vital 2020-06-28 09:00:11 UTC
Created attachment 1795 [details]
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.
Comment 1 moonlightsentinel 2020-06-28 09:57:35 UTC
You need to use “in” on your struct, not the string array.

“box” in cs;
Comment 2 Vital 2020-06-28 11:00:09 UTC
Oh, sorry ! 
Yes!
Comment 3 Stanislav Blinov 2020-06-28 12:09:33 UTC
...and it would be the opBinaryRight :)