-
-
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
use abstract|primitive type
, struct
, mutable struct
for type definitions
#20418
Conversation
As @simonbyrne pointed out during a discussion of this yesterday:
|
If we go the way of annotating individual fields as mutable, then |
Is this for v0.6? |
Yes --- otherwise it's unlikely to happen. |
base/abstractarray.jl
Outdated
immutable LinearFast <: LinearIndexing end | ||
immutable LinearSlow <: LinearIndexing end | ||
struct LinearFast <: LinearIndexing end | ||
struct LinearSlow <: LinearIndexing end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@StefanKarpinski said:
So its defining property is that it is made up of fields, not its mutability or lack thereof.
If one agrees with this perspective (I think it makes sense), then structs with no fields feels a little odd.
(I actually thought these lines seemed odd when I ran across them, and post-justified it with Stefan's comment because it fits.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zero is a valid number of fields :) Most (all?) languages with structs allow empty structs, and they're especially useful in languages that support various type system tricks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C/C++ don't, but I would hope that language is the exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust calls these "Unit Structs"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, empty structs are fine in C++ and commonly used for unique tags with which traits can be associated, etc. In an amusing quirk of the C++ standard, sizeof(boost::blank) != 0
:-(
Immutability is orthogonal to structs in rust (it's instead a property instead of the variable pointing to it, which is enforced by its linear type system, so probably not applicable here). |
The Rust documentation says "The values in structs are immutable by default" and the |
Is a |
No, I'm willing to rename |
I bet this could be done with sed but I made a small script that can update all the .jl files in a folder (recursively). It uses https://github.com/KristofferC/Tokenize.jl so you need to get that. The script is here: https://gist.github.com/KristofferC/24c269727d914ffb88bfc21624dad891
Standard caveats about backups, only run in a vm, read the code, don't use it at all etc. etc. apply. I ran it on |
👍 to the change.
But to clarify, is the deprecation warning (which seems to be implemented in the scheme code) more sophisticated than |
For cross-version compatibility, would I need to wrap my type definitions in an |
I often use a pattern where I have tons of keyword arguments to set the parameters of an algorithm (example), and then build a struct to keep them and pass them around (example). Most of the times, all or most of the parameters need to be mutable, as they can change during the algorithm execution. (I also have other patterns where I heavily use mutable fields, in iterative algorithms that solve complicated systems of equations, although I don't have a publicly available example of that at the moment.) If the implication of using Refs for mutable fields is that we'd end up sprinkling |
I don't think that's a danger. There would have to be enough magic to make this transparent, which means that it's largely an implementation detail. Also totally irrelevant to the rest of this discussion :) |
@timholy Yes it gives decent line numbers, I really mean it :) Deprecations that can be implemented in the parser give the best line numbers, because only the parser knows exactly where it is in the original file. After that point information starts to get lost. @KristofferC Thanks, that is awesome! |
I wonder if we could implement
|
Update: as I hoped, this PR stimulated a lot of discussion. @StefanKarpinski proposed using compound keywords, and many of us now favor:
This has a lot of advantages:
|
If I may, I'd like to explain my opposition to the multi-word keyword definitions proposed.
|
To rebut these points...
|
I see at least three possibilities for Compat with one-word names like
I would prefer the first option (no deprecation warning in this release.) Update: I didn't read far enough in this thread... I see that @JeffBezanson has an alternate proposal that would work with |
A language is also about aesthetics. When trying out Regarding upgradability I can't really assess, just thinking:
Special cases:
|
I have to say that with the two-word variants, You could also have |
It would probably be |
@swissr Very much agree aesthetics count. I think |
If you want to encourage people to default to using |
Is the proposal at the top of the PR obsolete?
to
is not an improvement in readability for me. |
Did some more renaming; should be in fairly good shape now. In updating docs I found the new keywords quite helpful; we can now easily distinguish immutable types (meaning anything that's immutable, e.g. tuple, struct, or primitive type), bits types ("plain old data"), primitive types, and structs. Probably even more improvements in doc, error message, and internal function names are now possible. The notepad++ mode seems to use a space-separated list of keywords, so I don't know how to handle the 2-word keywords there. For now I just threw in all the words. |
Agree – this is going to help with explaining things a lot. |
msg::AbstractString | ||
end | ||
|
||
#type UnboundError <: Exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it was ever used. Surprising how long those 3 lines managed to hang around.
Anybody understand the os x failure here? |
Looks like a timeout – it got hit with some signal and stopped. I restarted it. |
and now the log of the failure is gone. save to a gist before restarting on travis, please! copy paste the raw log link and run |
The log wasn't that interesting. We have tons of examples over at #17626. |
This case looked a little different to me, it was in the parallel test instead of spawn. |
introduced while #20418 was pending
Ah well, I apologize. Otoh, this PR got merged!! |
I think it would be great to put this change out as a blog post, perhaps even before the 0.6 release announcement. |
I noticed that latest doc (0.6.0-dev from December 07, 2016) https://media.readthedocs.org/pdf/julia/latest/julia.pdf |
we don't use readthedocs any more |
Is there a way to remove those files? It would be misleading to have them appear as "stable" or "latest" forever. |
So where can I download a PDF file of latest doc? |
Just want to say that this change is really convenient when searching for type definitions, e.g. when you don't know if the type is mutable or immutable; searching for |
-Range renamed AbstractRange JuliaLang/julia#23570 -squeeze renamed dropdims -immutable renamed struct JuliaLang/julia#20418
Fixes #19157.
This regularizes the type-defining keywords and makes them more accurately descriptive. The proposed syntax is
This is nicely extensible for the future, avoids stealing the words abstract, primitive, and (im)mutable as keywords, and as a bonus supports Compat.
struct
; previously there was no keyword that described what the most common kind of user-defined type actually was.===
, and it's really pretty rare to need to update object fields. Base has many more immutable types than mutable, and in making this change it seemed to me that even more types could be made immutable. Buttype Foo
looks more natural thanimmutable Foo
, sotype
tends to be the default choice. This change reverses that, withstruct Foo
being more natural, and immutable. Writingmutable struct Foo
makes you ask "does this really need to be mutable?", which is a good thing!struct
and makes them immutable. Not that we need to copy Rust, but it adds confidence that this is a reasonable thing to do.Ref
fields where needed in immutable structs.So far I really like the way this change looks.
immutable
is kind of a long and ugly word.The deprecation warning gives line numbers.
I haven't updated the manual yet. I think we'll want to reorganize it a bit to introduce immutable structs first.