-
-
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: separate array concatenation from array construction #7128
Comments
If breaking changes are being considered for this, I'll put in a vote for using an explicit delimiter here. Comma, semicolon, whatever. Not whitespace. |
Well, this would free up the ability to use an explicit delimiter instead of requiring using whitespace. It is still sometimes nice to use whitespace, although I guess we could avoid that since it's a bit of a parsing nightmare. |
Inconvenience of one extra character per element for explicit delimiters is minor compared to the parsing inconsistency IMO |
It's not really a perfect dup of either of those, although it is related. |
#3737 is an exact dup. Even the title is almost the same. |
|
As mentioned elsewhere, the |
+1 for making |
Consolidating from #7293, I'd like to see ways of constructing both N-vectors and Nx1 matrices, since we're distinguishing them. |
Reclaiming |
Perhaps it reminds you of C++11 initializer lists? |
I guess not, I don't even know what they are! I've not updated my knowledge of C++ in the last decade... |
FWIW, as someone who does more general-purpose work with Julia I disagree that the (And redundant ≠ useless – all of the current array syntax is essentially a syntactical convenience, but often those are important) |
I like the suggestion of using On Thu, Sep 18, 2014 at 9:11 PM, one-more-minute notifications@github.com
|
In general Julia does really well with its "never mention types when you don’t feel like it" philosophy – I really think it would be a shame to lose that. |
How crazy would it be for |
I'm going to go with pretty crazy. Which norm would you want (plenty to choose from)? Are norms commonly needed enough to deserve their own syntax? |
I feared as much, just wanted to bounce it off of sane people. Probably the |
wouldn't standalone |
That would apply to the original proposal as well, right? Of those, So is |
I don't think using |
Quite right... we have the current behavior at the parsing level, before even getting to what they do
(Wow the brackets wear so many hats. I'm not sure I have all of them listed.) New brackets could follow a simplified pattern, mapping into |
The unicode brackets I'd be content just to parse for now. But some alternative ascii brackets like |
I disagree that we should claim not to have space-sensitive syntax, and then actually have it within a special kind of string that we tell people to use. You still have space-sensitive syntax, plus the added problems of putting code in strings. This is an important language construct for us, so we ought to be able to parse it with just our normal parser. This makes life easier for writers of other macros or code analysis tools. If some syntax is worth having, it's worth having in the actual parser. |
One of the things which attracted me to Julia was the cleanliness of the language - it's pretty and (mostly) easy to comprehend even for someone who is not a scientist and who's programming background is in K&R C with little actual programmin over the last couple of decades. What however is not pretty and clean at all is putting code into strings. For me it turns code hard to read - that's just the way my brain is wired. Likewise using white spaces as separators is just hideous from readability point of view - I'd certainly prefer to see , and ; as mandatory separators. I'm also not sure it's a good idea to use the macro approach as macros tend to be a slight turn off for highly incompetent folks like myself, but it's certainly better from approachability point of view than the string thing. |
@Jakki42, would you think the string solution is not nice because it usually does not offer sensible syntax highlighting? In other words, would you be okay with the solution if we offer specialized syntax highlighting for it? |
String macros are like the wild west. The macro could do absolutely anything. Which is awesome and powerful, but also can be crazy and hard to reason about. Lots of folks have a rightfully-learned aversion to code in strings. And they're right. Code has no place in a string. It requires runtime parsing and evaluation, which is slow and a performance trap in almost all languages since there's no way for a static analyzer to reason about what the evaluation might result in. But string macros don't necessarily return code in a string. They can implement their own parsing rules and place the resulting expression directly into the surrounding code before it's compiled. There could potentially be no difference between the text of a source file and the text within a string macro. It's all just parsed text. As a toy example, here's a string macro that simply transforms its whitespace separated contents into an addition between all of them: julia> macro add_str(ex)
toks = map(parse, split(ex)) # This could be more robust, but works as a simple example
esc(Expr(:call, :+, toks...))
end
julia> x = 2; y = 3;
add"x y 2x*y sin(rand())"
17.775332435685616
julia> macroexpand(:(add"x y 2x*y sin(rand())"))
:(x + y + (2x) * y + sin(rand())) This means they can implement DSLs, add rich text markup, precompile regular expressions, execute C++ code, and more. A string macro's contents can be entirely data, or it can have its own interpolation rules (with code demarcated by After all this, I'm still with @JeffBezanson. If this is an important enough construct to be included in the standard library, it should be a first-class parsed syntax. All the other string macros included in base currently follow the "entirely data" or "data with interpolation" semantics — adding this would cause lots of confusion, I think. The only way I could see a consistent story here is if we decide that backtick string macros (#12139) should always contain in-scope Julian code and all other string macros should be predominantly string data (which may happen to be code for another language… which is unfortunate for |
@SimonDanisch - for me it's not a syntax highlighting issue, but the messyness of the syntax itself - it's confusing and unclear to me, a deviation from how the rest of the syntax looks like, like from a different language - I can not quicly glance though it, but my old and slow brain needs plenty of extra effort to understand what's going on. Maybe it is my background but to me [ ], ( ), { } clearly encapsulate something, make a unit or block of something and " " and ' ' always just seem like string or characher of no programmmatic meaning inside. While < > also form a nice opening and closing, I'm so old that to my brain they do not equate to anything to else but bigger or smaller than symbols and extra effort is needed to understand if they were to have something meaningful inside. And whitespace - to me it's always just something not relevant to syntax, parsed out. Anyhow, please keep in mind that I am a low end low priority user, certainly not a member of the main target audience groups of the language :-) |
I find the whole notion of string macros for doing math slightly horrifying. This is terrible syntax. It is not merely ugly and messy; it is ad-hoc and inconsistent. One principle of design is that similar things should look similar, and different things should look different. String macros should typically do string-like things. For example, PyPlot uses L"..." to LaTeX strings. String-like syntax should be used for string-like objects. By the same principle, collections of things like tuples and arrays should use [ ], ( ), { }. Another big problem with string macros, which was raised earlier, is that language syntax should be parsed by the parser. Macros force ad-hoc sub-languages inside Julia with their own alternate parsers. This is bad for tools that need to parse Julia (syntax highlighting was mentioned) and it is just bad design because macros in general are problematic. My biggest pet peeve with Julia is that @sprintf is a macro and not a function. Other languages manage to make sprintf functions, why can't Julia? So my view is that we need fewer macros in the core language, not more. |
@dcarrera your question about the |
We've addressed much of the core issue here by changing the meaning of |
Just wanted to register my strong preference that none of the array literals (including matrices) do any form of concatenation. The concatenation behavior feels like an unnecessary MATLAB-ism to me. If we're building Regarding syntax changes, the syntax that appeals most to me is the one mentioned by @yuyichao, with commas and semicolons and no whitespace sensitivity. I can see this generalizing arbitrary dimensions by having other symbols like double-semicolon # vector
[1, 2, 3, 4]
# matrix
[1, 2;
3, 4]
# 3D array
[1, 2;
3, 4;;
5, 6;
7, 8] The only thing is I sometimes wonder if it would be nicer to do this in storage order not "looks like linear algebra" order. Otherwise The final thing I'd love is a literal for 0-dimensional arrays, as I find |
+1 for the rules proposed by @andyferris, except for the 0D array part ( |
On the other hand |
Besides all the issues, whitespace sensitivity is so damn nicely lightweight 😢 [1 2 3
4 5 6
7 8 9] vs [1, 2, 3;
4, 5, 6;
7, 8, 9] |
There is no compelling reason to make a breaking change now |
Much gnashing of teeth derives from the overlap between syntax for array literal construction and array concatenation in Julia – largely inherited from Matlab. Perhaps we should just use a different syntax for block matrix construction entirely. One thought would be this:
This has the advantage of being pretty terse and lightweight. For example, the current idiom of expanding a range into an array is
[1:10]
which would become|1:10|
while[1:10]
would construct a one-element array of typeUnitRange{Int}
.The text was updated successfully, but these errors were encountered: