-
-
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
Syntax design bug: [a:b] #8950
Comments
Please stop opening duplicate issues. |
I believe this is a new bug report. Yes, they are closely related, but not really duplicate. |
This is definitely not a bug report. It's a complaint about existing syntax that is completely addressed by other issues. |
This is a design bug. |
It is quite clear from existing issues that everyone here is well aware of the issues surrounding current concatenation syntax and that the consensus is that it needs to be improved but that consensus hasn't yet been reached on the best way to fix it. It would be appropriate to make a comment on the most relevant existing issue rather than opening multiple new issues just to voice your specific suggestion. We've heard you: you don't like the fact that |
Sorry for my language. My view is that if one puts some things between "[" and "]", those things would be elements of an array. I opened this issue, because it is very likely that the solutions for other related issues would not change what "[a:b]" means as it is assumed that "[a:b]" is not a bug. |
You can use I think the disconnect here is that you're not realizing that the current behavior is because of concatenation. For example, julia> [1:3, 5:7]
6-element Array{Int64,1}:
1
2
3
5
6
7 concatenates the So @StefanKarpinski is entirely correct. |
I believe most of us don't realize the problem: julia> r = 1:5 julia> [1:5] julia> [r] timholy just assumed that the type of material between "[" and "]" must be known. |
julia> sort([1:2, 2:3]) julia> r1 = 1:2 julia> r2 = 2:3 julia> sort([r1, r2]) One would expect the outputs are arrays with two elements. |
Yes, this is a completely known behavior. I don't like it either. |
Like others have already said, you can get what you want by telling Julia to create an array of two ranges, instead of concatenating them:
It's not a "syntax bug", at the moment the expressions you use just don't mean what you want them to mean. |
Right, @xianrenb, there's nothing you're pointing out that hasn't been known for ages. It's just that no one has fixed it yet. Please, by all means, be the one to do so. |
julia> function push_stack(A::Array, item::Any) julia> function query_stack(A::Array) julia> a = [] julia> a = push_stack(a, 3) julia> a = push_stack(a, 4) julia> query_stack(a) julia> a = [] julia> a = push_stack(a, 1:2) julia> a = push_stack(a, 2:3) julia> query_stack(a) So I push a range but get an integer? |
julia> a = UnitRange{Int}[]
0-element Array{UnitRange{Int64},1}
julia> push!(a, 1:2)
1-element Array{UnitRange{Int64},1}:
1:2
julia> push!(a, 2:3)
2-element Array{UnitRange{Int64},1}:
1:2
2:3
julia> a[end]
2:3 |
What I want to show is that, with current design/implementation, programmers have to think carefully how to manipulate arrays without knowing the elements' type, or if the type is Any. |
Please fix it. |
julia> function init_with_single_item(item::Any) julia> size(init_with_single_item(1), 1) julia> size(init_with_single_item(2:3), 1) |
@xianrenb Admittedly, the current semantics, namely expanding arrays and performing concatenation within square brackets, is not ideal and sometimes causes confusion. However, it seems that it hasn't caused big problems in practice. I haven't seen lots of functions that have to introduce additional codes to deal with this. If your purpose is to write a function that can wrap an input item into an vector, no matter whether it is a number, a vector, or a range, you can already accomplish your goal with the status quo: # The function `singleton` wraps an item x to a vector containing exactly a single element `x`
singleton(x) = typeof(x)[x]
julia> singleton(1)
1-element Array{Int64,1}:
1
julia> singleton(2:3)
1-element Array{UnitRange{Int64},1}:
2:3 You can see that I don't have to introduce any branch statements to deal with the case where |
What about: singleton() or any other Julia function may not provide what a programmer wants. I believe changing line 497~521 of julia/range.jl is a good start, but this may change many things. Julia developers are creating a new computer language, and one of the targets should be easy to use. |
You don't need to keep repeating your point. In fact all of these things |
Perhaps this thread should be locked? It's clear that we're all in agreement. |
Thanks, @nolta. Haven't had time to follow everything that's been happening. |
Currently "[1:5]" represents "[1, 2, 3, 4, 5]".
But ranges are not the same as arrays!
"[1:5]" should not equal "[1, 2, 3, 4, 5]"!
I believe we need to be able to create array of ranges in a concise way. For example, "[1:5, 1:3]" should be an array of two ranges: "1 to 5" and "1 to 3".
Range(s) between "[" and "]" should still be range(s).
We could convert range to array: "convert(Array, 1:5)" would give "[1, 2, 3, 4, 5]".
It is clear that it is not a must-do for "[1:5]" representing "[1, 2, 3, 4, 5]".
So, why not consider "a..b" ?
Related issue: #8949
The text was updated successfully, but these errors were encountered: