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

docs for Base.promote_op #25609

Closed
simonbyrne opened this issue Jan 17, 2018 · 5 comments
Closed

docs for Base.promote_op #25609

simonbyrne opened this issue Jan 17, 2018 · 5 comments
Labels
doc This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch

Comments

@simonbyrne
Copy link
Contributor

Base.promote_op is used quite a bit through Base, mostly it seems for determining array output types, but it is undocumented. It would be nice to know (a) what it is supposed to do, and (b) where and when it can be used.

see also #25515 and #25591.

@ararslan ararslan added doc This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch labels Jan 17, 2018
@martinholters
Copy link
Member

(a) promote_op(f, argtypes...) guesses what an appropriate container eltype would be for storing results of f(::argtypes...). It invokes inference for coming up with the guess, so it can change its mind any time.
(b) It can safely be used whenever you don't care about the result (remember, it's just guessing). If you now think "but then I can never use it!", you got it. The only excuse to use it is if you don't have any elements to figure out their types and guessing is the best you can do. (E.g. broadcasting over empty input containers.)

Should I add that as a docstring to promote_op?

@martinholters
Copy link
Member

Oh, an extra warning I forgot: its guess will very often at least be a supertype of what f(::argtypes...) could return, so one might be lured into believing this is always the case, but it's not. So it cannot be used to determined the eltype of a container that is guaranteed to be able to store the results (yes, preallocation of an output array based on promote_op may lead to failure).

@simonbyrne
Copy link
Contributor Author

Yes, please open a PR.

@simonbyrne
Copy link
Contributor Author

Well, it is used for determining non-empty container types, e.g. for matrix multiplication.

@martinholters
Copy link
Member

Ref. #19669.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc This change adds or pertains to documentation types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

3 participants