JuliaLang/julia — pull request #1467 by carlobaldassi (2012-10-29 12:24:01+00:00)
Title: Add Dict comprehensions and typed Dicts

This would close #287.
Examples:

Dict comprehension:

```
julia> { i/2 => 2i for i = 1:4 }
{2.0=>8,0.5=>2,1.5=>6,1.0=>4}

julia> typeof(ans)
Dict{Float64,Int64}
```

Typed Dict comprehension:

```
julia> (Real,Real){ i/2 => 2i for i = 1:4 }
{2.0=>8,0.5=>2,1.5=>6,1.0=>4}

julia> typeof(ans)
Dict{Real,Real}
```

Typed Dict:

```
julia> (Real,Real){ 1=>2, 0x5=>2.0 }
{0x05=>2.0,1=>2}

julia> typeof(ans)
Dict{Real,Real}
```

In contrast to array comprehensions, the length of the iteration is not required:

```
julia> require("iterators.jl")

julia> import Iterators.*

julia> { i/2 => 2i for i in take(count(), 4) }
{0.0=>0,0.5=>2,1.5=>6,1.0=>4}
```

There are no restrictions on the number of iterators:

```
julia> { i+j => i-j for i=1:2,j=[4,8] }
{5=>-3,6=>-2,10=>-6,9=>-7}
```

Technical note: curly brackets preceded by an identifier parse a little different after this commit, since they use `parse-ref` rather then `parse-arglist`. I don't think this breaks any existing code (and of course `make testall` runs fine), but there are at least 2 notable differences:

1) 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 to `with-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.

2) the obscure semicolon-separated parameter syntax is unsupported, but I couldn't really find any place where it is actually supported:

```
julia> exp(1;2)
unsupported or misplaced expression parameters
```

(What is this about?)