Overview
| Artifact ID: | a045a1e6d4f9d4031113ec374bacfa40e971f57212197587babb10f2af8230a7 |
|---|---|
| Ticket: | 67bab20ac692c8e460b55a070888a6ddfbe1f1f6
if-construct: bizzare cases "without" body (mistake, typo, etc) |
| User & Date: | sebres on 2018-12-21 15:21:39 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-fossil-wiki"
- 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. 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> 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. - foundin changed to: "all"
- is_private changed to: "0"
- login: "sebres"
- priority changed to: "5 Medium"
- resolution changed to: "None"
- severity changed to: "Severe"
- status changed to: "Open"
- submitter changed to: "sebres"
- subsystem changed to: "17. Commands I-L"
- title changed to:
if-construct: bizzare cases "without" body (mistake, typo, etc)
- type changed to: "Bug"