-
-
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
Add Dict comprehensions and typed Dicts #1467
Conversation
For example: julia> { i/2 => 2i for i=1:5 } {2.0=>8,0.5=>2,1.5=>6,2.5=>10,1.0=>4}
This may be about the fact that a long time ago we planned to allow both optional and named arguments, and the separation between the two was to be indicated with a semicolon: foo(a, b=2; c, d=4) = a+b+c+d In this example:
I'll confess that I was the one behind this overly complex scheme and managed to convince Jeff and Viral for a little while that we should do this. We've subsequently come to our collective senses and decided this is way too much. |
Also, very nice patch. Obviously, we should wait for Jeff's feedback on this. |
Very well done as usual! This could even be extended to allow I wonder if it is confusing to put what are essentially type parameters in parens instead of the usual curly braces. Maybe it should be It's a good point that it would be better to allow assignments in macro arguments; that should be fixed. |
How about
Or perhaps i'm just being a syntax hypochondriac ;) |
Thanks! Stefan, I don't actually find that syntax that bad at all... About using About allowing While I'm at it, I'll confess that I have an experimental branch where I completed the (massively breaking) transition to using curly notation for Dicts exclusively, but looking at the result (older version here) made me actually change my mind on the this subject: on one hand the one and only gain would be in building empty Dicts; on the other hand, cell arrays are so ubiquitous and handy that having a special notation for them seems very relevant, plus there seem to be no real substitute for cell matrices (of which I found an instance in the sources, BTW). |
Ah, I like |
Yes, I also rather like |
I just had a horrible/ridiculous idea: |
Ok, I am fine with And it really is necessary for literal notations to support the empty case, so |
I'm a bit uncomfortable with indexing into anything with curly braces since that kind of syntax is already used for type parameters and feel like we're getting into fiddly syntax subtleties. How about instead indexing with square brackets and arrows: i.e. adding support for |
Examples: (Int=>Int){} (Integer=>Real){1=>2, 3=>3.0} (Real=>Integer){ i/2 => 2i for i = 1:3 }
for consistency, also allows (T=>S){=>}
Ok, for now I've changed the syntax to
In another commit, I added the syntax I actually support using square brackets for explicitly typed containers as per Stafan's suggestion, but having |
This would close #287.
Examples (updated with changes following the discussion):
Dict comprehension:
Typed Dict comprehension:
Typed Dict:
In contrast to array comprehensions, the length of the iteration is not required:
There are no restrictions on the number of iterators:
Technical note: curly brackets preceded by an identifier parse a little different after this commit, since they use
parse-ref
rather thenparse-arglist
. I don't think this breaks any existing code (and of coursemake testall
runs fine), but there are at least 2 notable differences:assignments are allowed: while this is not really a problem here, I think it would be reasonable to add a wrapper macro
with-assignment-allowed
, similar towith-end-symbol
etc. ; as a side effect, this could also be used to parse macro arguments and allow things like@options(x=1)
, unless there are plans to support named macro arguments.the obscure semicolon-separated parameter syntax is unsupported, but I couldn't really find any place where it is actually supported:
(What is this about?)