-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it easy to write literal dictionaries #592
Comments
I like the general idea of having a compact form for expressing dictionaries, particularly as the normal Dictionary construction is clunky, even more so than the I really don't like the |
I agree with you about the Pharo inconsistency. But in any case you should assert all elements are associations. Time ago I wrote something close to the idea, clean but it requires parentheses, and implement
The best would be: Other option using the current Array pattern: Are you comfortable with any of them? :) |
I like your "best" option as well from the point of view of it being compact and not confused with regular Smalltalk. I'm wary of adding new Smalltalk syntax to the compiler as it goes against the spirit of the language really. I think it has to have proven general worth to justify the reduction in simplicity that results from any addition of more syntax. That said, I'd support any effort to add a JSON format, including adding a compiler for it that could come into play for particular tagged methods. I'm less keen on an inline escape into it (e.g. using your extra pair of curly braces), but not implacably opposed. By using an alternate and well known format that is deliberately not Smalltalk we get the freedom to use different semantics. In another language we can express constant, or partially constant, structures recognised by its compiler, which would allow one to avoid dynamic execution just because that is what you have to do in order to obey the rules of Smalltalk where everything (with very few historic exceptions) is expected to be a message send, including those #<- operations to make an association out of two literal constants. Doing as much as possible at compile time to create largely constant data structures is important for many uses I think. One good example is as a replacement for STL to replace its use for view resources. STL is an (unreadable) literal format that then requires quite slow interpretation to build things, although it is completely general purpose. The current alternative options of performing a lot of dynamic/imperative construction at runtime can make everything slow in a way that is very difficult to optimize- you don't really want a peanut-buttering effect, i.e. an overhead on everything that adds a performance drag one can't eliminate, because it is a little bit here, a little bit there, adding up to a lot. If the data is constant, it should be expressible as constant literals, or at least much as possible. It is also undesirable to create a load of intermediate objects that are discarded - the most common form of hashed-key lookup collection in Dolphin is actually As I said, I'm not keen on the It strikes me that the object that knows how to consume the array of information is the target collection class, and we should be sending a message to it that asks it to produce an instance from a known input. What you are really saying, expressed in long form is Having written all that, and thought about it, I'd rather see a compiled JSON solution. In the meantime I'd just write:
This works already, although has a number of the qualities I don't like:
One could add the |
@blairmcg thanks as usual for your detailed explanation. I agree with you about everything. I'm not too worried about introducing a bit of new syntax to Smalltalk while it doesn't introduces secondary effects. The json syntax seems a bit problematic with the colon and comma characters hanging around. As for the alternatives you've mentioned I like
This has the convenience that we don't have to implement |
@fxgallego, are template strings the same as interpolated strings in C#? It looks like they are. Yes, I've often thought those would be nice to have. Its one of those things pending replacement of the compiler - it could be done in the current compiler, but it is hard to extend. I really must get back to finishing off #119. |
Yes, the same as interpolated strings. But the JS syntax is lighter, it uses only backticks and thus more suitable for smalltalk, I think. |
I'm playing with web things and I would like to have some syntax for writing literal dictionaries.
The new Dolphin's literal dynamic arrays, like {3+4. 'Hello' reverse}, could be an easy way to solve this.
We could write {'Hello'->'World'. 'Dolphin' -> 'Smalltalk'} asDictionary/asHash/asMap. Even the syntax looks very nice. I have to admit that I would have loved to still have the opportunity to use {} characters for hashes only, but on the other hand, Pharo uses the same syntax.
Another option to avoid a message send is to use double curly brackets.
Pharo implements
#asDictionary
in Collection using#associationsDo:
for the magic but in Dolphin that wouldn't be possible because the wayArray>>associations
are built.What do you think @blairmcg ?
The text was updated successfully, but these errors were encountered: