Skip to content
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

comprehensions element type: syntax extension proposal #614

Closed
carlobaldassi opened this issue Mar 20, 2012 · 2 comments
Closed

comprehensions element type: syntax extension proposal #614

carlobaldassi opened this issue Mar 20, 2012 · 2 comments

Comments

@carlobaldassi
Copy link
Member

If I got it right, comprehensions default to arrays of type Any whenever the type of the elements cannot be inferred at the time of parsing.
Apart from the issues with global variables (#524), there seems to be some unexpected behaviour in other cases, too:

[i | i = 1 : 4 ]
[i | i = 1 : (2 * 2) ]

all yield Vector{Int}, but

[i | i = 1 : 2^2 ]

yields Vector{Any}. It is easy to forget that a conversion is needed after a comprehension, and I suspect that building Array{Any}'s just to convert them later is a waste of resources.

Maybe it would be useful to provide some syntax to instruct the parser about what type to expect for the newly created array elements; for example, mimicking mathematical notation { i ∈ ℕ | ... } something like [ i in Int | ... ]. The keyword in fits nicely in that it is already taken, and its meaning is non-ambiguous for the parser, coming right before the | marker. The parser would discard the information if redundant (i.e. if it can already infer the type by itself, thus keeping the current behaviour), or trigger element-by-element conversions for concrete types, or just attach type-assertions for abstract types.

@JeffBezanson
Copy link
Member

Optional declarations already do exactly this:

julia> [i::Int | i = 1 : 2^2 ]
[1, 2, 3, 4]

julia> typeof(ans)
Array{Int64,1}

So no new features are needed.

@carlobaldassi
Copy link
Member Author

Oops, I did try that before but got an error because of a typo! Sorry :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants