Overview
| Artifact ID: | 2778ed2003efa0cbc6e11f57fcdb2c912a05860e68b4ec71d104b2815d32b131 |
|---|---|
| Ticket: | 67bab20ac692c8e460b55a070888a6ddfbe1f1f6
if-construct: bizzare cases "without" body (mistake, typo, etc) |
| User & Date: | sebres on 2018-12-21 15:33:01 |
Changes
- comment changed to:
Initially found by me (after some mistake/typo in code) as possible "legal" basic construct, like <code>"if"</code> without body, but with <code>"else"</code>: <code><pre style="padding-left:10pt"> if {[catch { set no error }]} else {puts "negative case only"} </pre></code> or even still simple: <code><pre style="padding-left:10pt"> if 0 else {puts "negative case only"} </pre></code> It is clear a mistake (but very unobvious if one just take a look at code), because <code>"else"</code> will be to body and the real body of <code>"else"</code> remains as negative case body (just because <code>"else"</code> operand is optional in <code>"if"</code>-construct). This way it is similar to: <code><pre style="padding-left:10pt"> if 0 {...} {puts "negative case only"} </pre></code> Obviously, it works this way without any problem, until expression results in true (where it will always fail, because trying execute <code>"else"</code> as body). <code><pre style="padding-left:10pt"> % if 0 else {puts "negative case only"} negative case only % if 1 else {puts "negative case only"} <b style="color:red">invalid command name "else"</b> </pre></code> The command is implemented this way possibly since the beginning (tested and reproducible with Tcl >= 8.2.2). Discussing the issue in tcl-chat, we've get an idea to "fix" this bizarre corner-cases without harm of current code, and improve it retaining "backwards" compatibility (for some possible obscure old code-pieces with <code>"else"</code>-only and expressions currently always resulting in <code>"false"</code>, therefore still working until today. Remain <code>"else"</code> as optional keyword, but if this is provided as body (as 3rd argument, without <code>"then"</code>) it will be recognized as keyword of <code>"if"</code>-command. <code><pre style="padding-left:10pt"> if $cond else {foo} </pre></code> would be then valid (short) replacement for empty body cases like: <code><pre style="padding-left:10pt"> if $cond {} else {foo} if $cond then {} else {foo} </pre></code> Following examples show how it may work: <code><pre style="padding-left:10pt"> % if 0 else {puts "negative case only"} negative case only % if 1 else {puts "negative case only"} <none> % proc else args {puts "else command $args"} % if 1 then {else} else command % if 1 then <b style="color:red">wrong # args: no script following "then" argument</b> % if 1 else <b style="color:red">wrong # args: no script following "else" argument</b> </pre></code> There are another keywords like <code>"elseif"</code> which could be handled similar way (for consistency reasons).<br/> Here is an example (provided in chat from avl42), which would call <code>foo</code> only if both <code>$cond1</code> and <code>$cond2</code> were false: <code><pre style="padding-left:10pt"> if $cond1 elseif $cond2 else {foo} </pre></code> and would be then proper replacement for: <code><pre style="padding-left:10pt"> if $cond1 then {} elseif $cond2 {} else {foo} </pre></code> I've a branch covering this "bug", prevents mistakes like this resp. improves the readability and consistency of (compiled and not-compiled) <code>"if"</code>-command in Tcl.<br/> WiP: working on the test-cases now. - login: "sebres"
- mimetype: "text/plain"