-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Dict syntax is getting me down #6739
Comments
I've often wanted this too. What would the type of => be? Pair? Having a tuple of pairs as a lightweight Dict-like type would be really handy too. Or something like that. |
+1, it'd be great to have |
It'd be great to address this with #7941. |
Absolutely. Personally I want a syntax that generalizes, as described above, but all the better if if plays along well with array syntax. |
I propose
Another option is to make |
Shouldn't that be |
A couple other things to think about in accordance with other proposed changes:
|
Dict(a=b) could be allowed, but only works for symbol keys. I really don't want special syntax using various combinations of brackets |
I really think that |
If a=>b produces a special type, then why not make it DictPair? Then the -erik On Sun, Sep 28, 2014 at 2:09 AM, toivoh [email protected] wrote:
Erik Schnetter [email protected] |
I think it could be OK for Another advantage @jakebolewski (IIRC) pointed out is that a |
use Dict(iter) or Dict(::Pair...) as Dict constructors ref #6739
Under development here: https://github.com/JuliaLang/julia/tree/jb/dictsyntax After trying this briefly, I am instantly in love with it. |
Another use for the func(a,b,c=>1) = .... A breaking change which offhand doesn't bring real benefits, but does seem more consistent. |
I'm excited about the pair idea. |
Lines 110 to 113 in 8c60aac
|
Oh, I see, you already introduced another |
@timholy Ha, yes, of course. Should have thought of that. Keyword arguments are not just pairs, but passed differently, by name instead of position. #7704 is related: in |
@JeffBezanson is is possible for |
Yes that is possible. Doing that would be a bit easier if we got rid of the |
I am sure there would be many uses of a |
A Pair type might be nice for specifying graphs in terms of their edges. |
Indeed, or the nodes of a binary tree, where every pair contains new pairs, up to the nodes at the lowest level, which contain the leaves. |
This Pair is like a tuple in that the types of both elements are tracked --- you wouldn't want to use it to represent a dynamic tree structure. |
I don't people are suggesting this be the final data structure, just that it's convenient sugar for specifying literal graphs. |
Yes, that's a fair point. It could well be useful to use the syntax for other data types. |
The Pair for a Dict should not be iterable. If you want a reflexive data structure, then a tuple would be best, no need to invent a new type Pair for this. What we want here for Dict (and other, similar cases) is a Pair with a bit more structure, a type that implies a "from--to" relation or somesuch. Maybe we could use
|
@elcritch I think your macro may actually work. Also the typed version doesn't really seem a relevant comparison. As for a written guide for porting v0.3 to v0.4 code, https://github.com/JuliaLang/Compat.jl serves as both a way to make code run deprecation-warning-free on both, but is at this point also the defacto guide. I think once we have a 0.4 prerelease there will interest in a transition guide. End-users shouldn't be playing with 0.4, its too unstable, so its kind of a low priority. |
Yes I think such a |
@JeffBezanson you mean the |
@ScottPJones , a |
@IainNZ , Thanks I'll look into it. Does that module also show how to allow macro to suppress syntax warnings? @JeffBezanson , the macro is really straightforward, but I'm unsure if it'd be more preferable to use traditional JSON notation e.g. |
@JeffBezanson , thanks the NEWS.md pointed me to the syntax change. Unfortunately it was unclear if the |
I'm confused --- Dict comprehension syntax (
|
Ooops! My bad for skimming, I must have interpreted it backwards. Here's a gist to an example macro json_macro. |
@elcritch, it would be great if you submitted a PR to JSON.jl. |
@elcritch The problem is that Julia's string syntax is not really compatible with JSON string syntax. (I had to deal with this myself a year ago, writing a JSON parser for another language). |
That has little to do with @elcritch's macro or request. AFAIK, we don't validate many of the strings/inputs we currently convert to JSON either, and in either case, that could (and should) be handled by a separate (optional) validation step in JSON.jl. |
What I was suggesting is that both cases would (and should) be handled by adding validation to JSON.jl (assuming that's where his macro would live). |
OK, yes, I fully agree with adding this all to JSON.jl. I'll start looking at what can be done to handle JSON string constants (possibly just within a json"""...""" string macro, if that's not already done) |
@ScottPJones JSON.jl doesn't have any macro. |
@ScottPJones , that'd be great! Having a JSON string macro would be useful for the cases you mention (e.g. direct compatibility). It sounds like you have experience with parsing JSON spec, would be possible for you to check the JSON.jl package and see if they're handling some of the more obscure cases correctly? I'll work on creating a PR for them. |
Having JSON as a subsyntax of Julia is not a goal. Having special syntax
for dictionaries in languages with a very privileged built-in dictionary
type makes sense. In Julia, it's just another data structure and not any
more special than user-defined or package-provided data structures like
OrderedDict, SortedDict, TreeDict, etc. So having special syntax ends up
being problematic and annoying rather than helpful.
|
@StefanKarpinski I agree, people tried to do the same thing to the language I worked on. You don't see a problem with having a json"""...""" string macro in JSON.jl though for convenience, do you? |
No, a json string macro seems ideal for this.
|
At least the |
While not having a special dictionary syntax is annoying at first (primarily since I've been using Python for so long), I completely agree that it actually ends up being a very annoying problem. For example, try adding a first class OrderedDict to Python. :/ @ScottPJones I could work on adding a macro string in a PR using the standard parsing method if you wanted to focus on the validation parts. I'd like to be able to contribute something, even if it's small. :) |
And fantastic point @ScottPJones regarding how Julia makes it possible. Great work @StefanKarpinski and @JeffBezanson and the rest of the team! |
https://github.com/JuliaLang/JSON.jl seems to be mostly the work of @WestleyArgentum and @aviks, very nice people I met at JuliaCon, so hopefully they will help us out! We should probably move this discussion to a PR under JSON.jl (can you open one there, @elcritch?) Macros are not (yet 😀) my forté, so I'd be happy just to deal with the validation parts. |
Great, will do! |
What was the reason for deprecating the {k=>v} syntax? I liked it and having to write Dict(..) every time seems kind of clunky. |
@axsk that ship has sailed long ago, your question is better suited at julia-users. Long story short, concistency, Julia is not Python and it uses |
@axsk, note that you can still do julia> [a=>b for (a,b) in zip(1:10,11:20)]
Dict{Int64,Int64} with 10 entries:
7 => 17
4 => 14
9 => 19
10 => 20
2 => 12
3 => 13
5 => 15
8 => 18
6 => 16
1 => 11 |
I have not been enjoying that
=>
is not first-class. It works well for simple dict literals ([a=>b, c=>d]
), but the other forms cause trouble. For example I definedbut one cannot make a Dict of this type using this definition. You have to write
(Symbol=>LatticeElement)[...]
every time.In the near future it will be possible to write
Dict( (a,b) for i in x )
, which makes a pretty good dict comprehension syntax without any special code in the front end. For literals this would beDict([(a,b), (c,d), ...])
, which starts to suffer from too much punctuation. One way to solve this is to make=>
a type, so you could writeDict(a=>b, c=>d, ...)
.=>
would not be iterable, so this would not be ambiguous. This would eliminate all special dict syntax. It would also make it more obvious how to write a typed empty Dict: you just pass 0 arguments.The text was updated successfully, but these errors were encountered: