From: cattaghia on 25 Apr 2010 16:51 hehehe! :) Well, anyway, now I think that "string is list" is not so reliable. But why does "dict exists" raise an error? This is an undocumented behaviour, and the purpose of the command is exactly to avoid an error from a future "dict get"... Fabricio
From: tom.rmadilo on 26 Apr 2010 01:28 On Apr 25, 1:30 pm, "Donal K. Fellows" <donal.k.fell...(a)manchester.ac.uk> wrote: > On 25 Apr, 21:03, cattaghia <cattag...(a)ig.com.br> wrote: > > As Gerald said, "most strings are lists"... I can't see how Tcl can > > state that "Tom is a cat" is a string and not a four-elements list. > > Oh, but it *is* a four-element list! And a string as well at the same > time. Types are merely a matter of perspective. "So what I told you > was true... from a certain point of view." "A certain point of view?" > "Luke, you're going to find that many of the truths we cling to depend > greatly on our own point of view." Is it even possible to test if a sting could be a dict? I've never seen such a flexible structure as a tcl dict, and I don't think this is a bad thing at all. But if we impose structure by feeding a string to a list command, or an array command, dict seems to require/allow a recursive dose of the usual structural interpretations. What that means to me is that you shouldn't be guessing (with a generic test) if a sting is a dict, because this really tells you nothing. Any particular dict command could be correctly invalid, or incorrectly valid given a valid dict, so a test for a valid dict is mostly useless.
From: Donal K. Fellows on 26 Apr 2010 04:11 On 26 Apr, 06:28, "tom.rmadilo" <tom.rmad...(a)gmail.com> wrote: > Is it even possible to test if a sting could be a dict? I've never > seen such a flexible structure as a tcl dict, and I don't think this > is a bad thing at all. But if we impose structure by feeding a string > to a list command, or an array command, dict seems to require/allow a > recursive dose of the usual structural interpretations. Syntactically, a dictionary looks like list (any list) subject to one or two extra conditions. The *strict* condition of well-formedness is that the list must have an even number of elements. The loose condition is that the key elements (i.e., first, third, fifth, etc.) should all be unique within a particular dictionary; this condition is never enforced by anything parsing a dictionary, but instead only the last such key/value pair is used. (Why? Ultimately because that was easiest to implement.) My [string is_dict] from earlier in this thread enforces the strict conditions of looking like a list and having an even number of elements, on the assumption that it is being used as a check to see whether the value can go into a [dict] operation without causing a syntax error. (Canonicality - which for dicts is equivalent to canonicality for lists plus the no-dup-key condition - is a much stricter and more expensive requirement, and one that the string from earlier in this thread would still pass.) > What that means to me is that you shouldn't be guessing (with a > generic test) if a sting is a dict, because this really tells you > nothing. Any particular dict command could be correctly invalid, or > incorrectly valid given a valid dict, so a test for a valid dict is > mostly useless. Yes. It's probably not a good idea to accept a dictionary directly from general user input. Donal.
From: cattaghia on 26 Apr 2010 18:31 Hmm. So I presume that "dict exists" checks this pairs "strict condition" on its argument before actually looking for a certain key. If the list does not have an even number of items, boOoOom. Not bad, really. But should not the documentation tell about the possibility of an error if a bad-formatted dict is thrown on it? Fabricio
From: tom.rmadilo on 26 Apr 2010 20:23 On Apr 26, 3:31 pm, cattaghia <cattag...(a)ig.com.br> wrote: > Hmm. So I presume that "dict exists" checks this pairs "strict > condition" on its argument before actually looking for a certain key. > If the list does not have an even number of items, boOoOom. > > Not bad, really. But should not the documentation tell about the > possibility of an error if a bad-formatted dict is thrown on it? I'm not a real user of dict, but I have run a number of experiments. Basically it is a portable, hierarchical Tcl array. But only the top level can be tested for an odd number of values !(len & 1). For instance dict x: {a {b c} d {e f g} h {i j k l}} passes the top level test. Path {a b} exists, path {d e} is invalid, path {h k} is valid. If you [dict lappend x d m], then path {d e} is now valid and returns "f". Not clear to me is why the [dict lappend x d m] doesn't create a dict {a {b c} d {{e f g} m} h {i j k l}}, but that is the way it works.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Recommended parser / lexer generator? Next: Bug in sqlite3? |