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

make Base.setindex() public #55129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

aplavin
Copy link
Contributor

@aplavin aplavin commented Jul 15, 2024

setindex() is a widely used well-documented function, it should be marked public, right?

Not sure what's the right way to make a function with multiple methods public, please correct me if I'm wrong here!

@giordano
Copy link
Contributor

I'm missing the point of this PR, as far as I can tell this is already public:

% julia +nightly -q
julia> Base.ispublic(Base, :setindex!)
true

@aplavin
Copy link
Contributor Author

aplavin commented Jul 15, 2024

Note the difference:

julia> Base.ispublic(Base, :setindex!)
true

julia> Base.ispublic(Base, :setindex)
false

base/tuple.jl Outdated Show resolved Hide resolved
@nsajko nsajko added collections Data structures holding multiple items, e.g. sets feature Indicates new feature / enhancement requests needs docs Documentation for this change is required labels Jul 17, 2024
@aplavin
Copy link
Contributor Author

aplavin commented Jul 21, 2024

Not sure what's the status here. Should I do some update of this PR or ...?

@nsajko
Copy link
Contributor

nsajko commented Jul 21, 2024

If setindex is to be made public, it should be documented, i.e., its doc string should be included into the docs. Thus the "needs docs" label.

@aplavin
Copy link
Contributor Author

aplavin commented Jul 22, 2024

I'm not familiar with documentation generation, could use some pointers on where to update things :)
Of course, assuming that this change is approved.

@nsajko

This comment was marked as outdated.

@nsajko
Copy link
Contributor

nsajko commented Jul 22, 2024

If the function is to be made public, we'd need to decide if it's just for tuples, i.e, will adding methods for other collection types be allowed. Perhaps we could instead define something like setindex_tuple(t::Tuple, v, i) = setindex(t, v, i) and then make only setindex_tuple public.

Adding new methods would negatively affect abstract return type inference in the extreme case where the type of the input collection is unknown (inferred as Any), quite possibly requiring the ecosystem to add some type asserts for performance.

@ararslan ararslan removed their request for review July 23, 2024 01:44
@ararslan
Copy link
Member

(Removing my review request as @nsajko has got this handled 😉)

@aplavin
Copy link
Contributor Author

aplavin commented Jul 24, 2024

Not sure what you exactly the issue with extending setindex and what exactly you suggest, @nsajko. Currently, setindex is defined (& documented) in Base for tuples and namedtuples. It's also used by lots of packages, with many more methods defined for setindex – eg, StaticArrays do that.

As the semantics of the function is pretty clear, and it's already extensively used in the wild (including new method definitions), what specific issues do you see with marking it public?


Perhaps we could instead define something like setindex_tuple(t::Tuple, v, i) = setindex(t, v, i) and then make only setindex_tuple public.

Tbh, I don't see a reason to do that. Defining separate functions for every type, instead of methods of the same functions, goes directly against Julia multiple dispatch design and practice.

@nsajko
Copy link
Contributor

nsajko commented Jul 24, 2024

I'm referring to the "world splitting" optimization, see the Base.Experimental.@max_methods doc string. I agree that Base.setindex should probably be available for all collection types. But, it's good to consider the possible downsides upfront, seeing as, in a fresh REPL session, Base.setindex has just three methods - below the max_methods default.

EDIT: I only mentioned this issue for completeness. IMO it's secondary to proper documentation.

@@ -151,6 +151,7 @@ Base.modifyproperty!
Base.setpropertyonce!
Base.propertynames
Base.hasproperty
Base.setindex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask, Is there a logic to this specific location, between hasproperty and getfield? Mostly just curious, I don't really have a better suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, perhaps the doc string would fit better into the Collections and Data Structures page? Under Indexable Collections I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properties/fields and indices are kinda close conceptually, and they are exactly the same for tuples and namedtuples (the types setindex is defined for). So it seems to make sense to put it here.
But any location works for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it can't go under "Indexable collections" as-is, because of the "fully implemented by" list in that section. The list includes types which don't implement setindex...

@nsajko
Copy link
Contributor

nsajko commented Jul 24, 2024

FWIW, given that setindex is indeed already used in many packages for some reason, and its interface seems OK, I now agree that it should probably be made public.

@aplavin
Copy link
Contributor Author

aplavin commented Jul 24, 2024

I'm referring to the "world splitting" optimization, <...>

Still not sure how these considerations are relevant to marking setindex as public.
I don't propose adding any more methods in Base here, although, I would like that as a completely separate independent PR (#43155, #54508, #43338, #33495). Marking it as public should just reflect the practical status quo: this function is widely used & overloaded.

Btw, a lot of high-performance code uses StaticArrays, and with them loaded one has at least 6 setindex methods.

@aplavin
Copy link
Contributor Author

aplavin commented Jul 30, 2024

bump...

@aplavin
Copy link
Contributor Author

aplavin commented Aug 12, 2024

bump :)

@aplavin
Copy link
Contributor Author

aplavin commented Aug 21, 2024

bump... anything else I should do here?

@aplavin
Copy link
Contributor Author

aplavin commented Sep 2, 2024

and another bump

@aplavin
Copy link
Contributor Author

aplavin commented Sep 19, 2024

bump, anyone?..

@aplavin
Copy link
Contributor Author

aplavin commented Oct 3, 2024

should be a tiny change, any issues with it?..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets feature Indicates new feature / enhancement requests needs docs Documentation for this change is required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants