Thread overview
Conflict with sumtype.match
6 days ago
Andrey Zherikov
6 days ago
monkyyy
5 days ago
Andrey Zherikov
6 days ago

I have a template similar to sumtype.match:

--- 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:

--- 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)

6 days ago

On Friday, 8 August 2025 at 04:40:14 UTC, Andrey Zherikov wrote:

>

I have a template similar to sumtype.match:

--- 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:

--- 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

--- 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

--- 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);
}
5 days ago

On Friday, 8 August 2025 at 05:19:28 UTC, monkyyy wrote:

>
--- 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);
}

I like this option because it doesn't require changes in user's code (main.d). But the issue with this implementation is that match_ doesn't have S parameter so it is not an equivalent to the original problem.