Created attachment 1795
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 by moonlightsentinel — 2020-06-28T09:57:35Z
You need to use “in” on your struct, not the string array.
“box” in cs;
Comment #2 by vital.fadeev — 2020-06-28T11:00:09Z
Oh, sorry !
Yes!
Comment #3 by stanislav.blinov — 2020-06-28T12:09:33Z