{"id":3633,"date":"2020-04-15T07:36:12","date_gmt":"2020-04-15T12:36:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3633"},"modified":"2020-08-21T07:43:55","modified_gmt":"2020-08-21T11:43:55","slug":"who-or-what-wins-er-takes-precedence","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2020\/04\/15\/who-or-what-wins-er-takes-precedence\/","title":{"rendered":"Who or What Wins, er, Takes Precedence?"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><!--introduction--><p>We've worried over the years as we build more features into MATLAB, particularly when we new file types and data types, about making sure users are accessing the artifacts they intend to use.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#f91435a6-1ab5-46e8-a51a-2908750b7ddf\">Variable and Function Precedence<\/a><\/li><li><a href=\"#0a28f19e-250d-43f8-9068-ae53b9587178\">Class and Operator Precedence<\/a><\/li><li><a href=\"#53e08072-b32b-47c9-9097-d523ad09341e\">An Example<\/a><\/li><li><a href=\"#8f4f6ad9-2633-4a7f-8db2-f32717427186\">One More Notable Change with Anonymous Functions<\/a><\/li><li><a href=\"#43b0098d-e224-4d69-a70d-c571235dfaf3\">Who Wins?<\/a><\/li><\/ul><\/div><p>We've even had commands available to help you identify what's going on, such as<\/p><div><ul><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/which.html\">which<\/a><\/tt><\/li><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/who.html\">who<\/a><\/tt><\/li><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/what.html\">what<\/a><\/tt><\/li><li><tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/exist.html\">exist<\/a><\/tt><\/li><\/ul><\/div><p>Or <tt>why<\/tt>?  Actually just kidding about the last one being helpful here.  But you might enjoy trying this:<\/p><pre class=\"codeinput\">why(174)\r\n<\/pre><pre class=\"codeoutput\">Loren knew it was a good idea.\r\n<\/pre><h4>Variable and Function Precedence<a name=\"f91435a6-1ab5-46e8-a51a-2908750b7ddf\"><\/a><\/h4><p>How does MATLAB decide, when it sees a name, whether to use a variable or which of several possible functions with the same name.  MATLAB has rules of precedence, and always has. In Release R2019b, the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html?category=language-programming&amp;rntext=precedence&amp;startrelease=R2019b&amp;endrelease=R2020a&amp;rntype=incompatibility&amp;groupby=release&amp;sortby=descending&amp;searchHighlight=precedence\">precedence rules<\/a> were updated.   In fact, many o f you were not affected because these changes are somewhat esoteric and are not encountered in lots of code.  But when certain conditions occurred in the past, people occasionally got unexpected (from their mental model) behavior.  We have tried to fix that with these updated rules.  You can find the details in the release notes I noted here, or in these portions of the documentation.<\/p><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019b\/matlab\/matlab_prog\/function-precedence-order.html\">function precedence<\/a><\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019b\/matlab\/matlab_prog\/upgrade-code-for-r2019b-changes-to-function-precedence-order.html\">update code instructions<\/a><\/li><\/ul><\/div><p>You'll notice in that last link that we provide code examples of what you might have written before, what behavior you saw, how to rewrite it with the new rules, and what behavior you should now see. You'll also notice that the conditions which have changed revolve around variables, nested functions, local functions, and external functions.  Some of these changes also strongly promote writing clearer code, such as not naming a local variable and a local function with the same name.<\/p><h4>Class and Operator Precedence<a name=\"0a28f19e-250d-43f8-9068-ae53b9587178\"><\/a><\/h4><p>Here are some other instances where some additional precedence rules apply,though I note that these have not changed:<\/p><div><ul><li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/matlab\/matlab_prog\/operator-precedence.html\">operator precedence<\/a><\/li><li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2020a\/matlab\/matlab_oop\/class-precedence.html\">class precedence<\/a><\/li><\/ul><\/div><h4>An Example<a name=\"53e08072-b32b-47c9-9097-d523ad09341e\"><\/a><\/h4><p>Here are 3 files for one example.  We start with the code from 19a or earlier.<\/p><p>This function runs without error in R2019a, with 2 different meanings of <tt>local<\/tt>.<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">function<\/span> myfunc19a\r\nlocal(1); <span class=\"comment\">% local is a function<\/span>\r\nlocal = 2; \r\ndisp(local);\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<span class=\"keyword\">function<\/span> local(x)\r\ndisp(x)\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>This next code errors in R2019b.<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">function<\/span> myfuncErr19b\r\n<span class=\"comment\">% local is an undefined variable<\/span>\r\nlocal(1); <span class=\"comment\">% Errors<\/span>\r\nlocal = 2; \r\ndisp(local);\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<span class=\"keyword\">function<\/span> local(x)\r\ndisp(x)\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><p>And this code works fine.  And would also have run correctly earlier.<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">function<\/span> myfuncGood19b\r\nlocalFcn(1);\r\nlocal = 2; \r\ndisp(local);\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<span class=\"keyword\">function<\/span> localFcn(x)\r\ndisp(x)\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><h4>One More Notable Change with Anonymous Functions<a name=\"8f4f6ad9-2633-4a7f-8db2-f32717427186\"><\/a><\/h4><p>This is another change with respect to function behavior, specifically anonymous  functions.  As of R2019b,  anonymous functions can include both resolved and unresolved identifiers.<\/p><h4>Who Wins?<a name=\"43b0098d-e224-4d69-a70d-c571235dfaf3\"><\/a><\/h4><p>Hoping we all win, by having cleaner, clearer code, from which we can access the elements (variables and functions) that we are looking for. Did anyone run into issues with this change in R2019b or later?  If so, please let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3633#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_0ae76e266da1446e85412181f3464d32() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='0ae76e266da1446e85412181f3464d32 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0ae76e266da1446e85412181f3464d32';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2020 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_0ae76e266da1446e85412181f3464d32()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2020a<br><\/p><\/div><!--\r\n0ae76e266da1446e85412181f3464d32 ##### SOURCE BEGIN #####\r\n%% Who or What Wins, er, Takes Precedence?\r\n% We've worried over the years as we build more features into MATLAB,\r\n% particularly when we new file types and data types, about making sure\r\n% users are accessing the artifacts they intend to use. \r\n%%\r\n% We've even had\r\n% commands available to help you identify what's going on, such as\r\n%\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/which.html which>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/who.html who>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/what.html what>|\r\n% * |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/exist.html exist>|\r\n%\r\n% Or |why|?  Actually just kidding about the last one being helpful here.  But you\r\n% might enjoy trying this:\r\nwhy(174)\r\n%% Variable and Function Precedence\r\n% How does MATLAB decide, when it sees a name, whether to use a variable or\r\n% which of several possible functions with the same name.  MATLAB has rules\r\n% of precedence, and always has. In Release R2019b, the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html?category=language-programming&rntext=precedence&startrelease=R2019b&endrelease=R2020a&rntype=incompatibility&groupby=release&sortby=descending&searchHighlight=precedence\r\n% precedence rules> were updated.   In fact, many o f you were not affected\r\n% because these changes are somewhat esoteric and are not encountered in\r\n% lots of code.  But when certain conditions occurred in the past, people\r\n% occasionally got unexpected (from their mental model) behavior.  We have\r\n% tried to fix that with these updated rules.  You can find the details in\r\n% the release notes I noted here, or in these portions of the\r\n% documentation.  \r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2019b\/matlab\/matlab_prog\/function-precedence-order.html\r\n% function precedence>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2019b\/matlab\/matlab_prog\/upgrade-code-for-r2019b-changes-to-function-precedence-order.html update code instructions>\r\n%\r\n% You'll notice in that last link that we provide code examples of what you\r\n% might have written before, what behavior you saw, how to rewrite it with\r\n% the new rules, and what behavior you should now see. You'll also notice\r\n% that the conditions which have changed revolve around variables, nested\r\n% functions, local functions, and external functions.  Some of these\r\n% changes also strongly promote writing clearer code, such as not naming a\r\n% local variable and a local function with the same name.\r\n%% Class and Operator Precedence\r\n% Here are some other instances where some additional precedence rules\r\n% apply,though I note that these have not changed:\r\n%\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2020a\/matlab\/matlab_prog\/operator-precedence.html\r\n% operator precedence>\r\n% * <https:\/\/www.mathworks.com\/help\/releases\/R2020a\/matlab\/matlab_oop\/class-precedence.html\r\n% class precedence>\r\n%\r\n%% An Example\r\n% Here are 3 files for one example.  We start with the code from 19a or\r\n% earlier.\r\n%\r\n% This function runs without error in R2019a, with 2 different meanings of\r\n% |local|.\r\n%\r\n% <include>myfunc19a.m<\/include>\r\n%\r\n% This next code errors in R2019b.\r\n%\r\n% <include>myfuncErr19b.m<\/include>\r\n%\r\n% And this code works fine.  And would also have run correctly earlier.\r\n%\r\n% <include>myfuncGood19b.m<\/include>\r\n%\r\n%% One More Notable Change with Anonymous Functions\r\n% This is another change with respect to function behavior, specifically\r\n% anonymous  functions.  As of R2019b,  anonymous functions can include\r\n% both resolved and unresolved identifiers. \r\n%% Who Wins?\r\n% Hoping we all win, by having cleaner, clearer code, from which we can\r\n% access the elements (variables and functions) that we are looking for.\r\n% Did anyone run into issues with this change in R2019b or later?  If so,\r\n% please let me know <https:\/\/blogs.mathworks.com\/loren\/?p=3633#respond\r\n% here>.\r\n\r\n\r\n\r\n##### SOURCE END ##### 0ae76e266da1446e85412181f3464d32\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>We've worried over the years as we build more features into MATLAB, particularly when we new file types and data types, about making sure users are accessing the artifacts they intend to use.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2020\/04\/15\/who-or-what-wins-er-takes-precedence\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,40],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3633"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=3633"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3633\/revisions"}],"predecessor-version":[{"id":3816,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3633\/revisions\/3816"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}