-
-
Notifications
You must be signed in to change notification settings - Fork 706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Fix Issue 22736 - Add destructuring bind for std.typecons.Tuple tuples #8372
Conversation
|
Thanks for your pull request, @CyberShadow! Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#8372" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will defer the acceptance of this to @atilaneves
90ea124 to
936d4ef
Compare
|
I have a more general implementation of this in my personal "utilities" module that
I currently use the name Here's a gist with the code for my version: https://gist.github.com/pbackus/90c333fb17422e1e8477ab68214dbb3a |
|
That looks nice.
Structs' fields generally have names, so I think having it for tuples makes for a more compelling use case.
Unfortunately this does not allow restricting the template to The reason this is a top-level function and not a
I've been tempted to call so many things |
Both struct fields and tuple fields can be either named or (individually) anonymous. I don't see any reason to give
Yes it does. You just put
I'm familiar with the technique.
You're probably right. It's not a very suggestive name on its own if you don't get the Lisp reference. |
Because it has a much stronger use case, we can be more comfortable in claiming the word
No, it doesn't work. https://gist.github.com/CyberShadow/f099037e99b796d5058d2ca3fdb4078f
There's also other programming languages and probably other professional areas where such terms have specific meanings, so we should be considerate of those with other experiences. |
I'd argue that we should consider the implications of the name either way, but it is true that the cost of a mistake is a little lower for the less-general version. I agree that it would have live somewhere other than
In general, if you import symbols with the same name from two different modules, you may have to disambiguate. Fortunately, the language gives you plenty of tools to do so (qualified names, aliases, In practice, there are no other symbols in Phobos that Experience with |
936d4ef to
c1c0da2
Compare
|
I should mention that the strongest motivation for the "curried" version is use in range pipelines: someRange
.enumerate
.map!(bind!((index, element) => doSomethingWith(index, element)))
.each!doSomethingElse;With this PR's current implementation, the above usage is impossible, and one instead has to write .map!(tup => tup.bind!((index, element) => doSomethingWith(index, element))) |
IMHO, it seems less annoying to add Also, I would recommend to stay away from destructuring tuples of named items into a list, because |
If this is added as-is, there is a 100% chance I will be forced to endure the annoyance of writing
My hope here is that your (and Atila's) intent is not set in stone. :)
The same criticism could be leveled at literally any language feature that allows the programmer to bind names to values. I don't think trying to prevent this kind of mistake is (or should be) within In your specific example, |
Fortunately, we are moving away from such dangerous features; for function arguments, we've had
Sorry, I messed it up. D's typing is pretty lax anyway, what with implicit int/bool conversions etc.
I think the point is to not have to put types in every lambda's parameter list. |
|
As far as I know, there are no plans to deprecate the following code, which has exactly the same issues as your auto t = getCoordinates();
auto longitude = t.expand[0];
auto latitude = t.expand[1];The fact is, no matter what we do in It's also important to consider what kind of precedent would be set by adding a special case like this to |
Yes, but why in the world would you write it like that? That's downright maliciously obfuscating the intent, when the tuple already has perfectly good names you could use. It wouldn't pass code review, while the
Rejecting named tuples would already follow a different precedent, in which we allow a new feature for some very narrow subset of parameters first, when it's not clear which semantics to use for the rest. What to do with named tuples can be a separate debate; this pull request is for destructuring tuples with anonymous fields only. |
...and then people complain on the forums that D is full of half-finished features. Maybe if we aren't sure of the correct semantics in all cases, this addition is not quite ready for Phobos yet, and would be better off taking some time to mature on code.dlang.org. |
|
Here is a suggestion:
|
|
Closed in favour of this PR. |
References:
destructuring-bindin Common Lisp (though here we only do flat tuples)t.bind!funis equivalent tot.expand.Identity!funImplementation TODO: