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
Agreeing on doc, and doc contribution style guide, about hash construction; {...}, %(...), hash(), ... #2117
Comments
|
El vie., 22 jun. 2018 a las 17:26, Juerd Waalboer (<notifications@github.com>)
escribió:
The problem
The style guide
<https://github.com/perl6/doc/blob/master/writing-docs/STYLEGUIDE.md>
suggests using {} for empty hashes and %() for non-empty hashes. This is
inconsistent.
It's not inconsistent, it's simply true...
Suggestions
Since {} is considered confusing (although technically it's supposed to
be straight forward) with blocks, and empty %() is considered a trap (see
rakudo/rakudo#1946 <rakudo/rakudo#1946>), why
not just use hash instead? According to jnthn in rakudo/rakudo#1946
(comment)
<rakudo/rakudo#1946 (comment)>
these are a "sure bet".
Suggested: avoid both %() (empty construct is a trap) and {} (possible
confusion with blocks) for constructing hashes, and always use hash
instead:
my $foo = hash; # empty hash
my $bar = hash a => 'b', c => 'd'; # non-empty hash
Well, this is the style guide. The main problem I see is that if we change
it now we might have to change a whole lot of stuff to make it consistent
with it. As a matter of fact, this change was made just today and we'll
have to go through what is there now to check that we're not initializing
using %().
On the other hand, well, this is the style guide. If we don't enforce
consistent style in it, such as the one you suggest, I don't know where
else could be...
I guess I'll have a look and see how much needs to be changed. And anyway,
thanks for the suggestion...
|
|
I'm completely against this. You'll simply not see code like that in the wild. People should be exposed to code somewhat like they will encounter in the ecosystem . And the {} %(...) distinction is fine and, besides a WAT at first, works just fine in practice. |
Well, is the documentation to be descriptive or prescriptive? The existence of a style guide, and the exclusion of |
|
By the way, if the style guide does want to prescribe the use of And then there are |
|
There are some interesting performance differences, too! Benchmark: Output: Going by the benchmark, &hash is the obvious winner, if you ignore the more obscure |
|
You forgot Benchmark (sorted), because it's interesting: |
|
El sáb., 23 jun. 2018 a las 5:20, Juerd Waalboer (<notifications@github.com>)
escribió:
You'll simply not see code like that in the wild. People should be exposed
to code somewhat like they will encounter in the ecosystem.
Well, is the documentation to be descriptive
<https://en.wikipedia.org/wiki/Linguistic_description> or prescriptive
<https://en.wikipedia.org/wiki/Linguistic_prescription>? The existence of
a style guide, and the exclusion of { ... } for hashes because it can be
confused with blocks, both strongly suggest prescriptivism. But your
argument is based on descriptivism. That's a fair point of view, and maybe
the example code in the documentation should indeed look like the code in
the ecosystem, but in that case the documentation should not shy away from
using { ... } for non-empty hashes.
I would say it's prescriptive. In most cases, there's no code "in the wild"
to draw from.
|
|
El sáb., 23 jun. 2018 a las 7:53, Brent Laabs (<notifications@github.com>)
escribió:
You forgot Hash.new and Hash(Empty) and Hash(()). Hash() returns Hash(Any)
which is not what we want. TIMTOWTDI!!!
Benchmark (sorted), because it's interesting:
my %: 0.104 wallclock secs (0.101 usr 0.000 sys 0.101 cpu) @ 9642364.694/s (n=1000000)
I would say this one is a winner. Maybe use this?
|
|
I just did at talk at TPC about PHP, so I've been talking about that language a bit lately. One of the things I discussed with people afterwards is that PHP is a better language now, because (among other things) you can use square brackets Everything suggested here is a move backwards in readability and style, including my own previous suggestions. So I propose |
|
While Please note my latest StackOverflow attempt at formulating the precise {} hash vs block DWIM rule. I'm going on a trip for a week, but if everyone is happy with it I will be happy to integrate it into the doc a couple weeks from now. Imo the right way forward for the docs is to show respect for Larry's DWIM. This is consistent with jnthn's perspective and the majority of existing community usage and, I think, preferences, though I acknowledge some strong opinions against it. (I return to this aspect at the end of this comment.) Perhaps the WAT side of this DWIM belongs on the traps page but I'm unsure. Please read the latest version of my Reddit discussion of this DWIM/WAT that complements my SO answer. Imo the right way forward for the docs contributor style guide is to recommend use of the DWIM in all cases except where documenting the
Imo this should be "Prefer the {} form of declaring hashes".
Imo this is inappropriately "disrespectful" to Larry's DWIM. (I get that the "disrespect" isn't emotive but just trying to be clear.)
Idiom is natural to a native speaker. I'd argue that's much more {} than %().
As we know, it unfortunately introduces confusion with %() which won't go away for a few years. See also my discussion of strange consistency with hash subscripts and ugliness in pair literals in my reddit comment linked above.
Switching guidance to be use of the DWIM will eliminate this exception. I understand that samcv and alex and others got burned over this dwim. I think that was down to poor doc. Perhaps my poor original SO answer didn't help. (Hopefully it's not so bad now.) I understand that what I'm saying almost certainly won't resonate for them. Getting burned a few times before the right mental model clicks leaves a lasting impression, especially if the right mental model still hasn't clicked. But I'm hopeful that Larry's view (which is that the DWIM made sense), and the majority newbie view (which is that the DWIM is fine if no one tells them about it but rather just exposes them to it), and the view of some old hands (jnthn finds the DWIM simple and natural, as do many others including me), and the evident widespread use of this DWIM, and the prospect that improved doc and community understanding will be in place, are persuasive that disrespecting Larry's DWIM isn't the way to go but rather embracing it, at least for now, until we perhaps revisit this in another few years once |
|
I posted my prior comment before reading the latest comments about speed. Those look dramatic and perhaps trump everything else. Perhaps {...} can be sped up? How is speed for :key{:a,:b} vs :key(%(:a,:b))? I still think it makes sense to generally use {} as a hash constructor in docs, when an explicit constructor is needed, because it reads very well. We can document %(...) as being way faster and avoiding the {} hash/block DWIM/WAT though having the %() WAT. |
|
But really, what is WAT? |
|
I'm not so sure. Looking at your SO answer, it's a huge explanation on when That said, most of the cases when I was burned were other way around – a block that I for some reason wanted empty, but got a hash instead ( |
|
My point is prescriptive, there's no real problem with using {...} for both Blocks and Hashes. It's a problem that only exists on the minds of people that had their perception warped by computer science. I mean no offense by this, everyone changes their perception because of their background. This discussion came up some time back and it was decided the current situation was fine, therefore I'm closing the bug with WONTFIX again. |
Link? |
|
@AlexDaniel Sorry, I can't find a link. The only thing I can say is that any seasoned Perl 6 developer is already burned out from this discussion. So, let me summarize what I think we already have a rough consensus about (if not, just tell): Hash.new() in the docs will never gonna happen. It's not idiomatic. Now, should the docs use {...} or %(...)? I think the former to expose people to it, which takes away a bit of surprise it might have. There's also no need to go into how it works extensively, I think. Just show it being used and tell people how to get the other result in case the compiler decides to do a WAT. We also don't go into how self-clocking works when explaining terms and operators, we don't go into how Grammars tie into the OO system when explaining it, don't go into Grammars when explaining regexes, and we also don't wonder into the MOP when explaining how to write objects, among many other examples. |
|
And if the chosen form is slower, that's a problem for rakudo developers to solve, we should torture the implementation in behalf of the user. There's no motive for the different forms to take different amounts of time, the compiler is smart enough to know they mean the same thing and to choose the faster implementation. |
|
So, I would say we have several options. I call a vote:
I'll leave this open for 48 hours, tally votes, and do whatever simple majority decides. Any other thing you want to propose, comment here. |
|
On regard of my vote, I have spend some time following the conversation(s), so will try to summarize things a little(maybe more for myself than for others though): Considering the above, my vote is for: |
By whom exactly? It's a very simple rule and if there's confusion around it, perhaps the rule should be reworded to be clearer.
FWIW, there's definitely a bug in there and that performance landscape will change once R#1951 is resolved.
I'm not aware of it being removed. I think you're confusing the coercers with all the magic shortcuts for
Why does it need to suggest anything? The docs are written by many programmers, each of whom use different styles, and the language's motto is There's More Than One Way To Do It. It's not overly awful for readers of the docs to be exposed to many features, rather than for the docs to mandate a particular style chosen as the "recommended" one by the 7 people in this discussion. |
|
@zoffixznet I meant the shortcuts for $/ will be removed, I wasn't clear. And in 6.e I think %() should create an empty Hash. |
|
Why should it make sense to have so many different forms of defining an empty hash? |
|
@abraxxa No need to tell them. |
|
@abraxxa That discussion is off topic here. We're discussing the documentation, not Perl 6 itself. |
|
Not giving a recommendation is not a good solution. |
|
Well, it's a recommendation for documentation writers. We can still
recommend, or not, anything on the place where we talk about hash
definition itself. Feel free to check it out and create an issue if it does
not look right.
|
The problem
The style guide suggests using
{}for empty hashes and%()for non-empty hashes. This is inconsistent.Suggestions
Since
{}is considered confusing (although technically it's supposed to be straight forward) with blocks, and empty%()is considered a trap (see rakudo/rakudo#1946), why not just usehashinstead? According to jnthn in rakudo/rakudo#1946 (comment) these are a "sure bet".Suggested: avoid both
%()(empty construct is a trap) and{}(possible confusion with blocks) for constructing hashes, and always usehashinstead:Note: these can be used with or without parentheses. Since that goes for most expressions, that could probably use its own recommendation in the style guide.
The text was updated successfully, but these errors were encountered: