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

reflection: add Base.infer_return_type utility #52247

Merged
merged 1 commit into from
Nov 23, 2023
Merged

Conversation

aviatesk
Copy link
Member

@aviatesk aviatesk commented Nov 21, 2023

This commit introduces Base.infer_return_type, a new reflection
utility which shares a similar interface with Base.return_types but
differs in its output; Base.infer_return_type provides a singular
return type taking into account all potential outcomes specified with
the given call signature. This function parallels Base.infer_effects
and the newly added Base.infer_exception_type, offering some utility,
especially in testing scenarios.

Base automatically changed from avi/exception_types to master November 21, 2023 11:55
@aviatesk aviatesk force-pushed the avi/base-return_type branch from c936a78 to 4c3a3bb Compare November 21, 2023 11:59
world::UInt=get_world_counter(),
interp::Core.Compiler.AbstractInterpreter=Core.Compiler.NativeInterpreter(world)) -> rt::Type

Returns the return type of the function call specified by `f` and `types`.
Copy link
Member

Choose a reason for hiding this comment

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

Probably should say "a supertype of the return type"

And seems like Base.infer_return_type would be consistent with the names of other similar queries?

Also might want to add a See also ref to Base.promote_op & friends so that people get directed to ways to infer these for generated functions, rather than just being warned that using from a generated function is an error

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably should say "a supertype of the return type"

Maybe "an inferred return type" would be better? "supertype" sounds like returning something that is strictly wider in the type lattice. "inferred" might be a bit ambiguous, but it can still effectively communicate the idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

And seems like Base.infer_return_type would be consistent with the names of other similar queries?

Yeah. Base.infer_return_type isn't consistent with Base.return_types, but we might want to rename Base.return_types to Base.infer_return_types instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

Also might want to add a See also ref to Base.promote_op & friends so that people get directed to ways to infer these for generated functions, rather than just being warned that using from a generated function is an error

Are you suggesting that when it's absolutely necessary to infer the return type from a generated context, we should lean towards using Core.Compiler.return_type? Or is the point more about using the docstring of promote_op as a means to highlight the nuances of using these reflection tools?

Copy link
Member

Choose a reason for hiding this comment

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

It is never allowed directly, but you can pass it as an argument to the generated function

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean the implicit world::UInt argument that is passed to the generator function? I'm not sure how promote_op relates to generated functions.

Copy link
Member

Choose a reason for hiding this comment

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

no, just as a real argument

@aviatesk aviatesk changed the title reflection: add Base.return_type utility reflection: add Base.infer_return_type utility Nov 22, 2023
This commit introduces `Base.infer_return_type`, a new reflection
utility which shares a similar interface with `Base.return_types` but
differs in its output; `Base.infer_return_type` provides a singular
return type taking into account all potential outcomes specified with
the given call signature. This function parallels `Base.infer_effects`
and the newly added `Base.exception_type`, offering some utility,
especially in testing scenarios.
@aviatesk aviatesk force-pushed the avi/base-return_type branch from 4c3a3bb to 3b9eebb Compare November 22, 2023 09:15
@aviatesk aviatesk merged commit 10d58eb into master Nov 23, 2023
8 checks passed
@aviatesk aviatesk deleted the avi/base-return_type branch November 23, 2023 04:28
mkitti pushed a commit to mkitti/julia that referenced this pull request Dec 9, 2023
This commit introduces `Base.infer_return_type`, a new reflection
utility which shares a similar interface with `Base.return_types` but
differs in its output; `Base.infer_return_type` provides a singular
return type taking into account all potential outcomes specified with
the given call signature. This function parallels `Base.infer_effects`
and the newly added `Base.infer_exception_type`, offering some utility,
especially in testing scenarios.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants