Posted to tcl by jima at Thu May 25 16:37:29 GMT 2017view raw

  1. Consider the following attempt to simulate a
  2. subset of lsort:
  3.  
  4. proc mylsort {{ordering -default ascii -switch {ascii dictionary
  5. integer real}} list} {
  6. puts "Sort mode $ordering"
  7. lsort -$ordering $list
  8. }
  9.  
  10. It works with usual input:
  11.  
  12. (test) 117 % set list {value some other thing}
  13. value some other thing
  14. (test) 118 % mylsort -dictionary $list
  15. Sort mode dictionary
  16. other some thing value
  17.  
  18. but fails when a dash is present [1]:
  19.  
  20. (test) 119 % set list {-value some other thing}
  21. -value some other thing
  22. (test) 120 % mylsort -dictionary $list
  23. wrong # args: should be "mylsort
  24. ?|-ascii|-dictionary|-integer|-real|? list"
  25.  
  26. whereas the original lsort has no problem:
  27.  
  28. (test) 121 % lsort -dictionary $list
  29. -value other some thing
  30.  
  31. It even breaks EIAS [1]:
  32.  
  33. (test) 122 % lindex $list 0
  34. -value
  35. (test) 123 % mylsort -dictionary $list
  36. Sort mode dictionary
  37. -value other some thing
  38.  
  39. The remedy to this as per TIP 457 is the -- switch:
  40.  
  41. (test) 124 % mylsort -dictionary -- $list
  42. Sort mode dictionary
  43. -value other some thing
  44.  
  45. This always works, but it puts the burden on the programmer to add -- at
  46. all invocations. The current core "lsort" shows that the arguments are
  47. never ambiguous, the assignment can be resolved by counting. The last
  48. arg is the list, everything before is treated as an option. This
  49. strategy always works if a fixed number of positional arguments is at
  50. the beginning and/or end of the argument list. If core "lsort" were to
  51. be implemented in TIP457, then ALL Tcl programs using lsort without the
  52. -- switch would become buggy with a data dependent bug.