Open zegkljan opened 7 years ago
Just a note: the the "old way" examples can also be a single line, as the lexicon constructor function takes alternating pairs for key values:
set arr to lexicon("a", list(1, 2, 3, 4), "b", lexicon("d", 1, "e", 2), "c", "x").
(Sorry, reading through old issues.) The reason this is a PITA to implement is because the braces are already the syntax for anonymous delegates, like so:
set x to { return 0. }.
So if this was also valid syntax:
set x to { "a": 0, "b": 1 }.
The parser would have no way of realizing which rule it was in when it hit the first open-brace token. It would have to wait until it got quite a ways into the syntax before it realized "well, let's see, string literal, colon, expression, comma.... Okay this isn't valid statement syntax so I guess this can't be an anonymous delegate so I guess it's a list of keys and values instead."
The Parser we use is only lookahead-1 so it can't really do that.
Maybe do not try to determine what curly brace contents mean at all. For lists, we could have something like:
L{123, 567, "abcd"}
and for lexicons something like:
X{"ab":10, "de":30}
This is similar to how some languages resolve potential type ambiguities in literals by, for example, allowing a double to be specified by 1234D
and a long to be specified by 1234L
.
It would be great if lists and lexicons could be created as literals, i.e. without using functions. The idea is that it should be similar to JSON or Python.
Lists example (based on kOS documentation example):
For lists, it is not as sexy since the standard constructor is already pretty ok but it would still save some typing, especially for nested lists.
Lexicons example (based on kOS documentation example):
Complex example:
Case-sensitive lexicons Lexicons, by default, are case insensitive but can be set to be case sensitive. The literals should support creating case-sensitive lexicons right away, without the extra step of setting the case sensitivity. A random solution to this could be adding some special character right after the opening brace: