<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div>I've tried to just say my piece and leave it at that (summary: not a fan but would be nice to have a way to match in fun heads/list comprehensions where variables are shadowed) but have to say that after seeing these examples I'm starting to lean towards agreeing with Richard.<br></div><div><br></div><div>On Thu, Jan 21, 2021, at 07:41, Richard Carlsson wrote:<br></div><blockquote type="cite" id="qt" style=""><div dir="ltr"><div>My own favourite quote by ROK is "an example would be useful about now", so in the spirit of that, I converted the OTP codebase to use ^-annotations everywhere, to get something concrete. This took me all of one afternoon: I rebuilt OTP from scratch with the warnings enabled, collected the warnings and converted them into a naive sed script that did the edits. Then I had to do some manual cleanup for those cases where my script had been too naive. Today I also took care of converting the test suites, which I missed in the first pass. (The test suites added about 2000 further uses of already-bound variables.)<br></div><div><br></div><div>To look at the diff, you can browse <a href="https://github.com/richcarl/otp/commits/pinning-otp">https://github.com/richcarl/otp/commits/pinning-otp</a><br></div><div>or if you prefer, fetch it to your otp repo:<br></div><div><br></div><div> git fetch <a href="https://github.com/richcarl/otp.git">https://github.com/richcarl/otp.git</a> pinning-otp<br></div><div><div> git log FETCH_HEAD<br></div><div><div dir="ltr" class="qt-gmail_signature"> git show FETCH_HEAD^ (for the normal modules)<br></div><div dir="ltr" class="qt-gmail_signature"><div dir="ltr" class="qt-gmail_signature"> git show FETCH_HEAD (for the testsuites)<br></div><div dir="ltr" class="qt-gmail_signature"><br></div></div><div dir="ltr" class="qt-gmail_signature"><br></div><div class="qt-gmail_signature">Note though that I did this as quickly as possible just to make it pass the build. To do a conversion like this for real, the annotated code should at least be superficially inspected by someone who knows it, because I think some of the existing uses are wrong or at least not quite what you want to do, and this would be your main chance to find and correct them.<br></div><div class="qt-gmail_signature"><br></div><div class="qt-gmail_signature">On the whole, I find the annotated code so much more readable (in those places where already-bound variables are used), that it's not even funny when I think about all the time lost over the years to staring at the code and trying to see which variables are already bound and how that affects the control flow. With ^-annotations, these uses stick out very clearly even when quickly scanning the code in less or git diff.<br></div><div class="qt-gmail_signature"><br></div><div class="qt-gmail_signature">Here follow some observations I've made from just looking at this diff for things that stand out as odd:<br></div><div class="qt-gmail_signature"><br></div><div class="qt-gmail_signature"><div>--------------------------------------------------<br></div><div>Multiple pinned variables on same line - immediately visible what's happening<br></div><div><br></div><div> receive<br></div><div> {'DOWN', ^Ref, process, ^Proc, _Info} -><br></div><div> badarg;<br></div><div><br></div><div> receive<br></div><div> {ssh_cm, ^SSH, {open, ^Chn, RemoteChn, {session}}} -><br></div><div><br></div><div>The alternative way with temporary variables and guards are much harder to follow:<br></div><div><br></div><div> receive<br></div><div> {'DOWN', Ref1, process, Proc1, _Info} when Ref =:= Ref1, Proc =:= Proc1-><br></div><div> badarg;<br></div><div><br></div><div> receive<br></div><div> {ssh_cm, SSH1, {open, Chn1, RemoteChn, {session}}} when SSH1 =:= SSH, Chn1 =:= Chn-><br></div><div><br></div><div><br></div><div>--------------------------------------------------<br></div><div>Some comments existed mainly because the use was not obvious:<br></div><div><br></div><div> #{Start:=FromPos} = SPos, %Assertion.<br></div><div><br></div><div> {Name, N, Reply} -> %% Name is bound !!!<br></div><div><br></div><div>Annotations are checkable comments:<br></div><div><br></div><div> #{Start:=^FromPos} = SPos,<br></div><div><br></div><div> {^Name, ^N, Reply} -><br></div><div><br></div><div><br></div><div>--------------------------------------------------<br></div><div>Some possible bugs immediately stand out when ^-annotated:<br></div><div><br></div><div> [L1,_L2|^Rest] when is_list(L1) -> ...<br></div><div><br></div><div>The rest of the list is alreay bound? Seems fishy.<br></div><div><br></div><div><br></div><div> case lists:keyfind(AppName, 1, StopRunning) of<br></div><div> {^_AppName, ^Id} -> ...<br></div><div><br></div><div>An underscore-prefixed variable which is already bound? Probably working by luck, not by intention.<br></div><div><br></div><div><br></div><div> parse_top(Line0, DecodeOpts, D) -><br></div><div> {Label,Line1} = get_label(Line0),<br></div><div> {Term,Line,^D} = parse_term(Line1, DecodeOpts, D),<br></div><div><br></div><div>Did the author really expect parse_term to return the same D here?<br></div><div><br></div><div><br></div><div> ^E0=processed_whole_element(S,Pos,Name,Attrs1,Lang,Parents,NSI,Namespace),<br></div><div><br></div><div>This whole line turned out to occur twice in the function body, but gets the same<br></div><div>result both times, thankfully.<br></div><div><br></div><div><br></div><div>--------------------------------------------------<br></div><div>Some weird code becomes obvious when annotated.<br></div><div><br></div><div>What does this line do?<br></div><div><br></div><div> _ = [M = M:module_info(module) || M <- Needed],<br></div><div><br></div><div>Oh, it's a multi-assertion!<br></div><div><br></div><div> _ = [^M = M:module_info(module) || M <- Needed],<br></div><div><br></div><div><br></div><div>--------------------------------------------------<br></div><div>Some uses indicate that you should probably have written this in a less cute way:<br></div><div><br></div><div> {M, ^F, A} = MFA = {cerl:atom_val(cerl:call_module(Guard)), F, length(Args)},<br></div><div><br></div><div>is easier to read and maintain as:<br></div><div><br></div><div> M = cerl:atom_val(cerl:call_module(Guard)),<br></div><div> A = length(Args),<br></div><div> MFA = {M, F, A},<br></div><div><br></div><div><br></div><div>--------------------------------------------------<br></div><div>Some cases are just very unclear if they are intentional or not, until you<br></div><div>have a full understanding of what the code does:<br></div><div><br></div><div> select_bin_seg(#k_val_clause{val=#k_bin_int{size=Sz,unit=U,flags=Fs,<br></div><div> val=Val,next=Next},<br></div><div> body=B},<br></div><div> #k_var{}=Src, Fail, St0) -><br></div><div> Ctx = get_context(Src, St0),<br></div><div> {Mis,St1} = select_extract_int(Next, Val, Sz, U, Fs, Fail,<br></div><div> Ctx, St0),<br></div><div> {Bis,St} = match_cg(B, Fail, St1),<br></div><div> Is = case Mis ++ Bis of<br></div><div> [#b_set{op=bs_match,args=[#b_literal{val=string},OtherCtx1,Bin1]},<br></div><div> #b_set{op={succeeded,guard},dst=Bool1},<br></div><div> #b_br{bool=Bool1,succ=Succ,fail=Fail},<br></div><div> {label,Succ},<br></div><div> #b_set{op=bs_match,dst=Dst,args=[#b_literal{val=string},_OtherCtx2,Bin2]}|<br></div><div> [#b_set{op={succeeded,guard},dst=Bool2},<br></div><div> #b_br{bool=Bool2,fail=Fail}|_]=Is0] -><br></div><div> ...<br></div><div><br></div><div>Is the use of Fail in the patterns above intentional and important?<br></div><div>There are no comments to guide you.<br></div><div><br></div><div>In the following, is the duplicated call to ssa_args/2 (with identical<br></div><div>arguments) and the match on the already bound As with the (hopefully same)<br></div><div>result of the second call intentional?<br></div><div><br></div><div> test_cg(Test, Inverted, As0, Fail, St0) -><br></div><div> As = ssa_args(As0, St0),<br></div><div> case {Test,ssa_args(As0, St0)} of<br></div><div> {is_record,[Tuple,#b_literal{val=Atom}=Tag,#b_literal{val=Int}=Arity]}<br></div><div> when is_atom(Atom), is_integer(Int) -><br></div><div> false = Inverted, %Assertion.<br></div><div> test_is_record_cg(Fail, Tuple, Tag, Arity, St0);<br></div><div> {_,As} -><br></div><div> {Bool,St1} = new_ssa_var('@ssa_bool', St0),<br></div><div> ...<br></div><div> end.<br></div><div><br></div><div>In the below, is the assertion intentional, or should it have been a new<br></div><div>variable "Plt1 = InitState#st.plt"?<br></div><div><br></div><div> get_warnings(Callgraph, Plt, DocPlt, Codeserver,<br></div><div> TimingServer, Solvers, Parent) -><br></div><div> InitState =<br></div><div> init_state_and_get_success_typings(Callgraph, Plt, Codeserver,<br></div><div> TimingServer, Solvers, Parent),<br></div><div> Mods = dialyzer_callgraph:modules(InitState#st.callgraph),<br></div><div> Plt = InitState#st.plt,<br></div><div> CWarns =<br></div><div> dialyzer_contracts:get_invalid_contract_warnings(Mods, Codeserver, Plt),<br></div><div> ...<br></div><div><br></div><div>Can you spot the assertion in the below?<br></div><div><br></div><div> do_init_trans_id_counter(ConnHandle, Item, Incr) -><br></div><div> case megaco_config:lookup_local_conn(ConnHandle) of<br></div><div> [] -><br></div><div> {error, {no_such_connection, ConnHandle}};<br></div><div> [ConnData] -><br></div><div> %% Make sure that the counter still does not exist<br></div><div> LocalMid = ConnHandle#megaco_conn_handle.local_mid,<br></div><div> Min = user_info(LocalMid, min_trans_id),<br></div><div> Max =<br></div><div> case ConnData#conn_data.max_serial of<br></div><div> infinity -><br></div><div> 4294967295;<br></div><div> MS -><br></div><div> MS<br></div><div> end,<br></div><div> Item = ?TID_CNT(LocalMid),<br></div><div> Incr2 = {2, Incr, Max, Min},<br></div><div> case (catch ets:update_counter(megaco_config, Item, Incr2)) of<br></div><div> ...<br></div><div><br></div><div>Is the assertion below intentional, or a left-over from some old refactoring?<br></div><div>(There are no other calls to write_to_store()).<br></div><div><br></div><div> ...<br></div><div> Oid = {Tab, element(2, Val)},<br></div><div> case LockKind of<br></div><div> write -><br></div><div> mnesia_locker:wlock(Tid, Store, Oid);<br></div><div> sticky_write -><br></div><div> mnesia_locker:sticky_wlock(Tid, Store, Oid);<br></div><div> _ -><br></div><div> abort({bad_type, Tab, LockKind})<br></div><div> end,<br></div><div> write_to_store(Tab, Store, Oid, Val);<br></div><div> ...<br></div><div><br></div><div> write_to_store(Tab, Store, Oid, Val) -><br></div><div> {_, _, Type} = mnesia_lib:validate_record(Tab, Val),<br></div><div> Oid = {Tab, element(2, Val)},<br></div><div> ...<br></div><div><br></div><div>This double file header check is probably intentional, but a comment would<br></div><div>have helped. With a ^-annotation on the second H0, the intent would have<br></div><div>been clear:<br></div><div><br></div><div> try dets_v9:check_file_header(FH, Fd) of<br></div><div> {ok, H0} -><br></div><div> case dets_v9:check_file_header(FH, Fd) of<br></div><div> {ok, H0} -><br></div></div><div class="qt-gmail_signature"><br></div><div class="qt-gmail_signature"><br></div><div class="qt-gmail_signature"><br></div><div dir="ltr" class="qt-gmail_signature"> /Richard<br></div></div><div><br></div></div></div><div><br></div><div class="qt-gmail_quote"><div dir="ltr" class="qt-gmail_attr">Den fre 15 jan. 2021 kl 13:34 skrev Richard Carlsson <<a href="mailto:carlsson.richard@gmail.com">carlsson.richard@gmail.com</a>>:<br></div><blockquote class="qt-gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;padding-left:1ex;"><div dir="ltr"><div>There have been many strong reactions in this thread, so let me give you some statistics to show how much this feature of using bound variables is actually used in practice. I checked the entire OTP codebase: there are just over 1300 modules, and in total about 595000 variable occurrences in patterns, of which only 3350 are already bound.. That makes 0.56% of all variables in patterns - about once in 200 to make it simple. On average, that's 2-3 usages per module - some modules using it more and some not using it at all.<br></div><div><br></div><div>I find it hard to see, then, why it should be a big issue to ask programmers to annotate these few occurrences for readability and maintainability. It's certainly not as big of a change as for example when the warning for unused variables, unless prefixed with _, was made the default.<br></div><div><br></div><div>Imagine a world where Erlang had not allowed already-bound variables in patterns (forcing you to use the idiom "X1 when X1 =:= X -> ...", as in e.g. Haskell), and that someone now came with the suggestion that to make things simpler, we could just implicitly match on the value of X if X is already bound. The old me from my university days would probably have said "that's really elegant, let's do it". But the maintainability-and-readability me, with experience of very large code bases, large numbers of developers, and many relative newcomers to the language, would say "aw hell no". This is a cute feature, but it carries a large cognitive cost and is not worth having compared to how relatively little it is used. Being explicit about intention is much more important.<br></div><div><br></div><div><div dir="ltr"> /Richard<br></div></div><div><br></div></div><div><br></div><div class="qt-gmail_quote"><div dir="ltr" class="qt-gmail_attr">Den tors 24 dec. 2020 kl 21:10 skrev Richard Carlsson <<a href="mailto:carlsson.richard@gmail.com" target="_blank">carlsson.richard@gmail.com</a>>:<br></div><blockquote class="qt-gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-color:rgb(204, 204, 204);border-left-style:solid;border-left-width:1px;padding-left:1ex;"><div dir="ltr"><div>The ^ operator allows you to annotate already-bound pattern variables as ^X, like in Elixir. This is less error prone when code is being refactored and moved around so that variables previously new in a pattern may become bound, or vice versa, and makes it easier for the reader to see the intent of the code.<br></div><div><br></div><div><div>See also <a href="https://github.com/erlang/otp/pull/2951" target="_blank">https://github.com/erlang/otp/pull/2951</a><br></div><div><div dir="ltr"><br></div><div>Ho ho ho,<br></div><div dir="ltr"><div><br></div><div> /Richard & the good folks at WhatsApp<br></div></div></div></div></div></blockquote></div></blockquote></div></blockquote><div><br></div></body></html>