Conflict with sumtype.match

monkyyy crazymonkyyy at gmail.com
Fri Aug 8 05:19:28 UTC 2025


On Friday, 8 August 2025 at 04:40:14 UTC, Andrey Zherikov wrote:
> I have a template similar to `sumtype.match`:
> ```d
> --- s.d
> struct S {}
> template match(A...)
> {
> 	auto match(S){return 1;}
> }
>
> --- main.d
> //import std.sumtype; // uncomment to get error
> import s;
>
> void main()
> {
>     S s;
>     assert(s.match!(_ => _) == 1);
> }
> ```
> If I uncomment `import std.symtype`, I get this error:
> ```
> /sandbox/main.d(7): Error: `match` matches conflicting symbols:
>     assert(s.match!(_ => _) == 1);
>             ^
> /sandbox/s.d(2):        template `s.match(A...)(S)`
> template match(A...)
> ^
> /dlang/dmd/linux/bin64/../../src/phobos/std/sumtype.d(1649):    
>     template `std.sumtype.match(handlers...)`
> template match(handlers...)
> ```
>
> So I'm trying to make my `s.match` to be applicable to `S` only.
>
> My idea was to move `match` inside `S`:
> ```d
> --- s.d
> struct S
> {
>   template match(A...)
>   {
> 	auto match(){return 1;}
>   }
> }
>
> --- main.d
> import std.sumtype;
> import s;
>
> void main()
> {
>     S s;
>     assert(s.match!(_ => _) == 1);
> }
> ```
> But after doing this, I got deprecation warning:
> ```
> /sandbox/s.d(5): Deprecation: function `main.main.match!((_) => 
> _).match` function requires a dual-context, which is deprecated
>     auto match(){return 1;}
>          ^
> /sandbox/main.d(7):        instantiated from here: `match!((_) 
> => _)`
>     assert(s.match!(_ => _) == 1);
>             ^
> ```
>
> How can I make this working without warnings and conflict with 
> `sumtype.match`?
> (I don't want to rename `match`)

you can give priority ez

```d
--- s.d
struct S {}
template match(A...)
{
	auto match(S){return 1;}
}

--- main.d
import std.sumtype; // uncomment to get error
import s;
alias match=s.match;
void main()
{
     S s;
     assert(s.match!(_ => _) == 1);
}
```

or following your other take, having the matching be in the 
struct but it forwarding  outside

```d
--- s.d
struct S
{
	auto match(A...)=match_!A;
}
template match_(A...){
	auto match_(){return 1;}
}
--- main.d
import std.sumtype;
import s;

void main()
{
	S s;
	assert(s.match!(_ => _) == 1);
}
```


More information about the Digitalmars-d-learn mailing list