Fix Issue 11067 - allow string handlers in std.sumtype.match#11071
Fix Issue 11067 - allow string handlers in std.sumtype.match#11071niy-ati wants to merge 4 commits into
Conversation
|
The current implementation breaks |
pbackus
left a comment
There was a problem hiding this comment.
I am strongly opposed to this, and would need a much stronger reason to convince me than "because other Phobos modules do it."
unaryFun is a workaround/hack from before D had built-in function literal syntax. While we cannot remove it from existing APIs without breaking backwards compatibility, there is no need to add it to new APIs. It offers no new functionality compared to built-in lambdas, and has several significant downsides (e.g., one cannot use imported symbols in the body of a string passed to unaryFun, because of how string mixin interacts with scoping).
|
I am in complete concurrence with @pbackus on this one. Phobos V3 will be removing the string handlers entirely and only support function literal syntax. This is retained in Phobos 2 for backwards compatibility only. |
|
String literal syntax is not deprecated. There isn't even documentation guidance that it should be avoided. Quite the opposite, the documentation references it heavily- probably because it reads a lot easier than lambdas. As it is in the language, it should be in the language uniformly. To be frank, I think the lambda syntax is not good for simple access expressions, as are very common in std.algorithm use. There's a reason that every doc example of "terse D" uses string form whenever it can. (edit: And there's a reason that every language with heavy lambda use evolves a shortcut field access syntax...) |
It's not deprecated; it won't be removed from PhobosV2. But it also shouldn't move forward into PhobosV3. |
|
I have no opinion yet on Phobos V3. This PR is to V2 however. |
|
okay update I just looked at the diff and I kind of regret raising the issue now. I was hoping we could just invoke I'm also not sure what you were trying to do with edit: I tested it locally and it seems to work? Not sure what the |
Apply std.functional.unaryFun/binaryFun inside matchImpl when matching and invoking handlers, instead of pre-wrapping all handlers at the call site. Keeps string handler support consistent with the rest of Phobos without a separate adapter system. Fixes dlang#11067
6f85fcf to
cba66d4
Compare
Use SumTypes.length instead of valueTypes!caseId.length when selecting unaryFun/binaryFun. In single dispatch valueTypes!caseId is a single type, not an AliasSeq, so .length was invalid there.
|
thanks everyone for the feedback , I've reworked the branch based on it. What changed now: Follow-up fix open to adjust further if anything still looks off. |
|
I think you're still misunderstanding the nArgs logic, or well one of us is. If the sumtype has 2 possible types, that doesn't mean the match callback will be called with two arguments. They're mutually exclusive. |
Drop handlerAlias/nArgs/binaryFun. Variant count is not handler arity; issue 11067 only needs single-dispatch string handlers, so always wrap with unaryFun inside matchImpl.
|
You're right @FeepingCreature about the variant/arity distinction ,I had muddled that in my explanation.
|
|
Oh you're right! Yeah, that's my bad. I totally forgot about the multi-sumtype matching because I have never used it. Mea culpa! That makes your template approach make a lot more sense tbh. |
|
Even with the current approach, using In practice I don't expect the 3+ case will come up very often, but it still needs to be documented, and inevitably someone will see it in the future and submit a PR adding |
Use unaryFun/binaryFun from handlerAlias based on SumType argument count (dispatch arity), with a clear error for string handlers on 3+ arguments. Document that string lambdas follow std.functional and are not supported beyond two SumTypes.
|
@FeepingCreature no worries, thanks for the correction. I've restored the @pbackus good catch on the 1/2 vs 3+ inconsistency. I've documented that |
|
@niy-ati Thank you for updating the documentation. My overall position on this PR has not changed. The entire rationale for adding this feature seems to be
I find (1) unconvincing because other Phobos modules only use Meanwhile, the downside of this PR is that it makes the implementation of |
Out of curiosity, would someone from the supporters crowd mind to benchmark this? |
|
I don't expect that this PR, on its own, will have a significant impact on compile times. The real problem (for which I take full responsibility, to be clear) is that |
|
I think D has a hard enough time convincing people to use it without forcing them to memorize valid expression syntax per call site. String syntax is supported. I happen to think it's good that it's in the language, (or rather, not exactly "good" but better than nothing), but independent of all that it's in the docs and in the examples and it works (I think) everywhere except here. If string syntax was marked as deprecated, legacy and unsupported that'd be something else, but it's not even documented as such in the docs page for |
|
The simplest approach for new D programmers is to never teach them the string syntax in the first place. This is what Programming in D does—see for example the chapters on ranges and lambdas, neither of which includes the string syntax.
I would be happy to submit a PR to fix this. |
|
Thank you. I mean, I like strings and wish they'd stay (wish they'd be replaced with better syntax, alas...), but either soft-deprecating or warning-off string syntax or this PR should be done. Anything but "we'll behave like they're deprecated but they're not deprecated and also it's not written down anywhere." |
That’s only fair. |
Summary
Apply
std.functional.unaryFun/binaryFuninsidematchImplwhen selecting and invoking handlers, so string forms likevalue.match!a.memberwork consistently with the rest of Phobos.Fixes #11067
Approach
Per review feedback: no separate string-handler adapter layer, and no pre-wrapping of all handlers at the
match/tryMatchcall site (that broke-betterCand misusedSumTypes.lengthas handler arity). Instead, handlers are adapted only insidematchImplvia a smallhandlerAliashelper that delegates tounaryFun(1 arg) orbinaryFun(2 args).Test plan
version (D_BetterC)-guarded unittest formatch!a.value