Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

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

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. 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"}
    &lt;none&gt;
    % 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.
    
  5. foundin changed to: "all"
  6. is_private changed to: "0"
  7. login: "sebres"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Severe"
  11. status changed to: "Open"
  12. submitter changed to: "sebres"
  13. subsystem changed to: "17. Commands I-L"
  14. title changed to:
    if-construct: bizzare cases "without" body (mistake, typo, etc)
    
  15. type changed to: "Bug"