-
-
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
checkcorrectness function for dict #8687
Conversation
This proposed unexported function, checkcorrectness, accompanies a proposed change that I just made to the documentation. As noted in a recent discussion on the julia user's group, https://groups.google.com/forum/#!topic/julia-users/bXyLf5v3uBE, the Dict indexing structure is corrupted if a mutable key gets changed by the user. The purpose of this function is to look for errors in a Dict in case the user inadvertently clobbers the index.
How about calling this |
This is a nice piece of code that seems to work well, but I'm really not sure how useful it is to have in Base. First, I just don't find this problem comes up very often. Second, this is a marginal kind of solution: you have to know that this function exists, and you have to obtain a reference to a suspected-invalid dictionary, and manually use this function. Having done that, you aren't much closer to finding the actual source of the bug. It seems like a strange kind of thing to burden the standard library with. |
I feel the same way. I can't imagine writing a program that would use this. If some value is getting mutated that is being used as a Dict key, I would copy the keys on insertion or extraction to avoid that instead. |
Jeff and Stefan, The point is that the dict keys might be changing unintentionally, i.e., In the near future Julia will get more complicated data structures (such Having said that, I agree that the toolbox does not necessary have to be On Wed, 15 Oct 2014, Stefan Karpinski wrote:
|
I don't really see what kind of code ends up mutating dictionary keys. FWIW I agree that eliminating this problem (e.g by having immutable versions of all data structures, and only allowing those as dict keys) would be ideal, but in practice using a value as a dict key makes it very unlikely to be subject to mutation. |
How about putting this in DataStructures.jl? There are already other Dict On Wednesday, October 15, 2014, Jeff Bezanson notifications@github.com
|
Complex data structures often have invariants, which are boolean If Base.Dict requires that keys don't change, and if this is not enforced In some cases, such invariants can even help the compiler produce better -erik On Wed, Oct 15, 2014 at 12:29 PM, Kevin Squire notifications@github.com
Erik Schnetter schnetter@cct.lsu.edu |
That is all true; I guess it's reasonable to have a standard name for checking complex invariants. |
Jeff, You challenged me to come up with a realistic example of how a user could However, I can at least dream up a scenario where a user could make this Consider writing a compiler for a language L with a complicated system of The author of this compiler represents each type as a Julia Array{Any,1} In order to recognize whether two types are the same, the author uses a Now he/she wishes to compactify the set of types that have been defined In this case, the compiler-writer might try the following: loop over the The problem with this loop is that if the compiler modifies a type T, and A rather convoluted example, but maybe it meets with your challenge! On Wed, 15 Oct 2014, Jeff Bezanson wrote:
|
That strikes me as a case where an object is mutated in such a way that it remains equal to its old value. No self-respecting compiler writer would ever genuinely mutate a type :) In any case, I think all we need to decide here is whether we want a generic function for checking complex invariants. If so, then this function is probably appropriate. |
Such structure-checking functions come up in plenty of other use cases - checking for duplicate or unsorted entries in a sparse matrix for example. Often they have application-specific names, but not always. For example there is currently an |
I think it's sufficiently clear this hasn't been necessary from the passage of time |
This proposed unexported function, checkcorrectness, accompanies a proposed change that I just made to the documentation. As noted in a recent discussion on the julia user's group, https://groups.google.com/forum/#!topic/julia-users/bXyLf5v3uBE, the Dict indexing structure is corrupted if a mutable key gets changed by the user. The purpose of this function is to look for errors in a Dict in case the user inadvertently clobbers the index.