<i>if-construct: bizzare cases "without" body (mistake, typo, etc)</i> status still Open with 3 other changes
by
.
D 2018-12-21T15:33:01.865
J comment Initially\sfound\sby\sme\s(after\ssome\smistake/typo\sin\scode)\sas\spossible\s"legal"\sbasic\sconstruct,\slike\s<code>"if"</code>\swithout\sbody,\sbut\swith\s<code>"else"</code>:\r\n<code><pre\sstyle="padding-left:10pt">\r\nif\s{[catch\s{\sset\sno\serror\s}]}\selse\s{puts\s"negative\scase\sonly"}\r\n</pre></code>\r\nor\seven\sstill\ssimple:\r\n<code><pre\sstyle="padding-left:10pt">\r\nif\s0\selse\s{puts\s"negative\scase\sonly"}\r\n</pre></code>\r\nIt\sis\sclear\sa\smistake\s(but\svery\sunobvious\sif\sone\sjust\stake\sa\slook\sat\scode),\sbecause\s<code>"else"</code>\swill\sbe\sto\sbody\sand\sthe\sreal\sbody\sof\s<code>"else"</code>\sremains\sas\snegative\scase\sbody\s(just\sbecause\s<code>"else"</code>\soperand\sis\soptional\sin\s<code>"if"</code>-construct).\s\r\nThis\sway\sit\sis\ssimilar\sto:\r\n<code><pre\sstyle="padding-left:10pt">\r\nif\s0\s{...}\s{puts\s"negative\scase\sonly"}\r\n</pre></code>\r\nObviously,\sit\sworks\sthis\sway\swithout\sany\sproblem,\suntil\sexpression\sresults\sin\strue\s(where\sit\swill\salways\sfail,\sbecause\strying\sexecute\s<code>"else"</code>\sas\sbody).\r\n<code><pre\sstyle="padding-left:10pt">\r\n%\sif\s0\selse\s{puts\s"negative\scase\sonly"}\r\nnegative\scase\sonly\r\n%\sif\s1\selse\s{puts\s"negative\scase\sonly"}\r\n<b\sstyle="color:red">invalid\scommand\sname\s"else"</b>\r\n</pre></code>\r\nThe\scommand\sis\simplemented\sthis\sway\spossibly\ssince\sthe\sbeginning\s(tested\sand\sreproducible\swith\sTcl\s>=\s8.2.2).\r\n\r\nDiscussing\sthe\sissue\sin\stcl-chat,\swe've\sget\san\sidea\sto\s"fix"\sthis\sbizarre\scorner-cases\swithout\sharm\sof\scurrent\scode,\sand\simprove\sit\sretaining\s"backwards"\scompatibility\s(for\ssome\spossible\sobscure\sold\scode-pieces\swith\s<code>"else"</code>-only\sand\sexpressions\scurrently\salways\sresulting\sin\s<code>"false"</code>,\stherefore\sstill\sworking\suntil\stoday.\r\n\r\nRemain\s<code>"else"</code>\sas\soptional\skeyword,\sbut\sif\sthis\sis\sprovided\sas\sbody\s(as\s3rd\sargument,\swithout\s<code>"then"</code>)\sit\swill\sbe\srecognized\sas\skeyword\sof\s<code>"if"</code>-command.\r\n<code><pre\sstyle="padding-left:10pt">\r\nif\s$cond\selse\s{foo}\s\r\n</pre></code>\r\nwould\sbe\sthen\svalid\s(short)\sreplacement\sfor\sempty\sbody\scases\slike:\r\n<code><pre\sstyle="padding-left:10pt">\r\nif\s$cond\s{}\selse\s{foo}\s\r\nif\s$cond\sthen\s{}\selse\s{foo}\s\r\n</pre></code>\r\nFollowing\sexamples\sshow\show\sit\smay\swork:\r\n<code><pre\sstyle="padding-left:10pt">\r\n%\sif\s0\selse\s{puts\s"negative\scase\sonly"}\r\nnegative\scase\sonly\r\n%\sif\s1\selse\s{puts\s"negative\scase\sonly"}\r\n<none>\r\n%\sproc\selse\sargs\s{puts\s"else\scommand\s$args"}\r\n%\sif\s1\sthen\s{else}\r\nelse\scommand\r\n%\sif\s1\sthen\r\n<b\sstyle="color:red">wrong\s#\sargs:\sno\sscript\sfollowing\s"then"\sargument</b>\r\n%\sif\s1\selse\r\n<b\sstyle="color:red">wrong\s#\sargs:\sno\sscript\sfollowing\s"else"\sargument</b>\r\n</pre></code>\r\n\r\nThere\sare\sanother\skeywords\slike\s<code>"elseif"</code>\swhich\scould\sbe\shandled\ssimilar\sway\s(for\sconsistency\sreasons).<br/>\r\nHere\sis\san\sexample\s(provided\sin\schat\sfrom\savl42),\swhich\swould\scall\s<code>foo</code>\sonly\sif\sboth\s<code>$cond1</code>\sand\s<code>$cond2</code>\swere\sfalse:\r\n<code><pre\sstyle="padding-left:10pt">\r\nif\s$cond1\selseif\s$cond2\selse\s{foo}\s\r\n</pre></code>\r\nand\swould\sbe\sthen\sproper\sreplacement\sfor:\r\n<code><pre\sstyle="padding-left:10pt">\r\nif\s$cond1\sthen\s{}\selseif\s$cond2\s{}\selse\s{foo}\s\r\n</pre></code>\r\n\r\nI've\sa\sbranch\scovering\sthis\s"bug",\sprevents\smistakes\slike\sthis\sresp.\simproves\sthe\sreadability\sand\sconsistency\sof\s(compiled\sand\snot-compiled)\s<code>"if"</code>-command\sin\sTcl.<br/>\r\nWiP:\sworking\son\sthe\stest-cases\snow.
J login sebres
J mimetype text/plain
K 67bab20ac692c8e460b55a070888a6ddfbe1f1f6
U sebres
Z d217ca2be7a14dc9d0057bba3ca158d1